module 04 command line arguments in c
DESCRIPTION
The presentation given at PCCOE, Nigdi, Pune by Prof. Tushar B Kute in workshop on "See through C". http://tusharkute.comTRANSCRIPT
See through C
Module 4Command Line Arguments
Tushar B Kutehttp://tusharkute.com
The main( ) function
● So far, we have been defining the main() function to receive no arguments.
● Actually, the main( ) function can receive two arguments.
● To do that, the main( ) function should be defined as below.
● This is how arguments can be passed in at the command line.
http://www.tusharkute.com 2
int main(int argc, char *argv[ ]) { … }
● From the command prompt, we can start running a program by typing its name and pressing ENTER.
● To pass arguments, we type in the program’s name followed by some arguments, then press ENTER.
● Below is an example from the Unix command prompt.
Command Line Arguments
% ./myprog% ./myprog 5 23
argc and argv
● When the program is called from the command line with arguments, argc will contain the number of arguments.
● When the user types in arguments, the user separates each argument with a space.
● argv is an array of character strings.● argv[1] is the character string containing the
first argument, argv[2] the second, etc.● Note: argv[0] contains the character string
that is the name of the executable/binary.
Example
int main(int argc, char *argv[ ]) {
int i;
for (i = 1; i < argc; i++) printf("%s ", argv[i]);
printf("\n");
return 0;
}
Thank you
This presentation is created using LibreOffice Impress 3.6.2.2