Chapter 6
Chapter 6
Chapter 6
p = fopen(“Prog.c”,"r");
q = fopen(“Prognew.c”,"w");
ch = getc(p);
while(ch != EOF)
{
putc(ch,q);
ch = getc(p);
}
printf("File is copied successfully. ");
fclose(p);
fclose(q);
return 0;
}
5. Explain file handing functions with example.
• C provides a set of functions to do operations on file. These functions are known as file
handling functions.
• Each function is used for some particular purpose.
the function.
• If reading is succeeded then it returns the number of variables that are actually assigned
values, or EOF if any error occurred.
Example: fscanf(fp, ”%d”, &sum);
else if ( ch == ‘ ’ )
words ++ ;
else
characters++;
c = getc ( fp );
}
fclose( fp );
printf(“Lines=%d Tabs=%d Words=%d Characters=%d”, lines, tabs, words, characters);
}
7. What is difference between Static Memory Allocation and Dynamic Memory Allocation
Static Memory Allocation Dynamic Memory Allocation
• If memory is allocated to variables • If memory is allocated at runtime (during
before execution of program starts execution of program) then it is called
then it is called static memory dynamic memory.
allocation.
• It is fast and saves running time. • It is bit slow.
• It allocates memory from stack. • It allocates memory from heap
• It is preferred when size of an array is • It is preferred when number of variables is not
known in advance or variables are known in advance or very large in size.
required during most of the time of
execution of program.
• Allocated memory stays from start to • Memory can be allocated at any time and can
end of program. be released at any time.
• The storage space is given symbolic • The storage space allocated dynamically has
name known as variable and using no name and therefore its value can be
this variable we can access value. accessed only through a pointer.
8. Explain Storage classes.
• Storage class decides the scope, lifetime and memory allocation of variable.
• Scope of a variable is the boundary within which a variable can be used.
• Four storage classes are available in C,
1) Automatic (auto)
2) Register (register)
3) External (extern)
4) Static (static)
1) automatic:
• Variables which are declared in function are of automatic storage class.
• Automatic variables are allocated storage in the main memory of the computer.
• Memory is allocated automatically upon entry to a function and freed automatically upon
exit from the function.
• The scope of automatic variable is local to the function in which it is declared.
• It is not required to use the keyword auto because by default storage class within a block is
auto.
Example:
int a;
auto int a;
2) register:
• Automatic variables are allocated storage in the main memory of the computer; However,
for most computers, accessing data in memory is considerably slower than processing
directly in the CPU.
• Register variables are stored in registers within the CPU where data can be stored and
accessed quickly.
• Variables which are used repeatedly or whose access time should be fast may be declared
to be of storage class register.
• Variables can be declared as a register: register int var;
3) external:
• Automatic and register variables have limited scope and limited lifetimes in which they are
declared.
• Sometimes we need global variables which are accessible throughout the program.
• extern keyword defines a global variable that is visible to ALL functions.
• extern is also used when our program is stored in multiple files instead of single file.
• Memory for such variables is allocated when the program begins execution, and remains
allocated until the program terminates. Memory allocated for an external variable is
initialized to zero.
• Declaration for external variable is as follows: extern int var;
4) static:
• static keyword defines a global variable that is visible to ALL functions in same file.
• Memory allocated for static variable is initialized to zero.
• Static storage class can be specified for automatic as well as external variables such as:
static extern int varx; //static external
static int var; // static automatic
• Static automatic variables continue to exist even after the function terminates.
• The scope of static automatic variables is identical to that of automatic variables.
9. Explain Input / Output functions.
1) scanf( )
• It is used to read all types of data.
• It cannot read white space between strings.
• It can read multiple data at a time by multiple format specifier in one scanf( ).
Example:
scanf(“%d%d”, &a, &b);
2) printf( )
• It is used to display all types of data and messages.
• It can display multiple data at a time by multiple format specifier in one printf( ).
Example:
printf(“a=%d b=%d”, a, b);
1) gets()
3) puts()
• It is used to display a string at a time.
Example:
char str[]=”Hello”;
puts(str );
4) putchar()
• It is used to display single character at a time.
Example:
putchar(ch);
Character checking functions.
Character checking functions are available in ctype.h header file.
1) isdigit(int); for checking number (0-9)
2) isalpha(int); for checking letter (A-Z or a-z)
3) isalnum(int); for checking letter (A-Z or a-z) or digit (0-9)
4) isspace(int); for checking empty space
5) islower(int); for checking letter (a-z)
6) isupper(int); for checking letter (A-Z)
7) ispunct(int); for checking punctuation symbols (like - : , ; , {, } , ?, . etc. )
10. Example: Write a program to check the entered character is digit or not
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c;
clrscr();
scanf("%c",&c);
7 Dept: CE ACP (3320702) Prof.Vishal K. Makwana
Unit 6 Files
if(isdigit(c))
printf("True");
getch(); }
11. Explain command-line argument with example.
• Command-line arguments are given after the name of a program in command –line operating
systems like DOS or Linux.
• Up to this point there is no argument in main() function.
• main() can accept two arguments :
1)First argument is number of command-line arguments.
2)Second argument is a full list of command-line argument.
Syntax:
int main(int argc, char *argv[])
• Here argc refers to the number of arguments passed and argv[] is a pointer array which point
to each argument which passed to main.
• Following is a simple example which checks if there is any argument supplied from the
command-line and take action accordingly:
Example:
#include <stdio.h>
int main( int argc, char *argv[] )
{
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
• Here argv[0] holds the name of the program itself and argv[1] is a pointer to the first
command line argument supplied, and *argv[n] is the last argument.
• If no arguments are supplied, argc will be one and if you pass one argument then argc is set at
2.
• You pass all the command-line arguments separated by a space, but if argument itself has a
space then you can pass such arguments by putting them inside double quotes "" or single
quotes ‘’.
Example:
$ ./a.out testing1 testing2
Too many arguments supplied.