Command Line Parameters
The empty paranthesis of function main() may contain special arguments that allow parameters to be passed to main() from the operating system level.
There are two such arguments, which are traditionally called argc and argv respectively.
The first of the argument argc must be an integer variable and the second argument argv is an array of pointers to strings. Each element in this array will represent a parameter that is passed to main().
The value of argc will indicate the number of parameters passed from the operating system level.
The execution of program is normally initiated by specifying the name of the program at the operating system level.
The program name is interpreted as an operating system command.
Hence the line in which it appears refers to as command line.
The parameters must follow the program name on the command line in order to pass one or more parameters to the program when the program execution is initiated.
The individual parameters must be separated by one another either by blank spaces or by tabs.
Blank spaces can be included within a parameter by enclosing the entire parameter within quotation marks.
The program name may be stored as the first item in argv followed by each of the parameters.
Hence, if the program name is followed by n parameters there will be n+1 entries in argv ranging from argv[0] to argv [n].
Moreover, argc will automatically be assigned the value n+1.
The parameter cmdarg2 is the filename of the program and cmdarg1.c is the name of the file of which we want to display the contents.
You can see the output we are reading the contents of the file cmdarg1.c (our earlier program).
Dynamic Memory Allocation<< Previous
Next >>Some C Programs
Our aim is to provide information to the knowledge seekers.
Support us generously