Main ( ) function
main( ) is the only function that every C program must contain. When a programmer runs a command to execute C Program the compiler of the C language searches for main ( ) function. It is the function from which the execution of the program begins.
The grammar of the compiler has code written that it should search for the main ( ) function to begin execution. When the compiler finds the main ( ) function it starts executing the code from the beginning of the main ( ).
main ( ) defines the memory address from which the compiler starts reading the programming instruction. Computer memory contains many operating system programs each having a specific memory address defining its beginning, to differentiate C language program from operating system program main ( ) function is used. main ( ) has a specific memory address defining its beginning.
Types of main function:
main ( ) function without parameters //without parameters int main( ) { … return ( 0 ); }
As it can be observed that the main( ) function has a return type int. It specifies that the program must return integer value to the calling program. If the main( ) function returns integer value 0 it specifies to the calling program that program execution was successful.
main ( ) with parameters int main(int argc, char * const argv[ ]) { …… return ( 0 ); }
This main function has parameters. This is called command-line arguments. Command line argument follows the name of the program at the command prompt. main ( ) contains two arguments: the first argument specifies the number of arguments and the second argument specifies the list of command line arguments.
argc (ARGument Count) is integer type and specifies the count of command-line arguments, count also include name of the program. When one parameter is passed to the main ( ) function the count of argc is 2 (1(name of the program) + 1(argument) = 2).
argv (ARGument Vector) is a character pointer array and contains all the arguments. argv contains pointers to strings. Value at index 0 of character array argv is the name of the program after that all the elements are command line arguments.
The name argc and argv can be renamed as per the requirement of the program but not its data type.
File handling in C is used to store data in a file through a program. File handling program in C language can create a new file, open a file, read data from file and write data to file. Command line arguments of main ( ) are used in file handling programs. Command line argument argc can be used to find the number of arguments passed to main( ) function and argv can be used to initialize the file pointer.
An example program illustrating the use of command line arguments in file handling is given below:
main ( ) function example
#include<stdio.h> int main(int argc,char *argv[]) { FILE *fs,*ft; int ch; if(argc!=3) { printf("Incorrect numbers of arguments."); return 1; } fs = fopen(argv[1],"r"); if(fs==NULL) { printf("source file not available."); return 1; } ft=fopen(argv[2],"w"); if(ft==NULL) { printf("Target file not opening."); fclose(fs); return 1; } while(1) { ch = fgetc(fs); if (feof(fs)) break; fputc(ch,ft); } fclose(fs); fclose(ft); return 0; }
Output: Incorrect numbers of arguments.
Code Analysis In the above code the main ( ) function has two command line arguments: argc integer type and argv [ ] character pointer array. In the if statement the number of arguments is checked. If the number of arguments is not equal to 3 messages is displayed by using printf statement. The code to do this is:
if(argc!=3) { printf("Incorrect numbers of arguments."); return 1; }
Second command line argument argv[ ] is used to set file pointer fs. File pointer is set to read mode “r”. Character pointer array argv[1] is used. This done by following code:
fs=fopen(argv[1],"r"); if(fs==NULL) { printf("Can't find the source file."); return 1; }
Next, the value at index 2 of the argument vector is used to set the file pointer to write mode. This done by following code:
ft=fopen(argv[2],"w"); if(ft==NULL) { printf("Target file not opening."); fclose(fs); return 1; }
The return type in main ( ) depends on the type of compiler being used. Compilers like C/C++ allow us to return nothing from main ( ), in this case instead of using return type integer void is used.
Leave a Reply