module 04 command line arguments in c

6

Click here to load reader

Upload: tushar-b-kute

Post on 15-May-2015

174 views

Category:

Software


0 download

DESCRIPTION

The presentation given at PCCOE, Nigdi, Pune by Prof. Tushar B Kute in workshop on "See through C". http://tusharkute.com

TRANSCRIPT

Page 1: Module 04 Command Line arguments in C

See through C

Module 4Command Line Arguments

Tushar B Kutehttp://tusharkute.com

Page 2: Module 04 Command Line arguments in C

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[ ]) { … }

Page 3: Module 04 Command Line arguments in C

● 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

Page 4: Module 04 Command Line arguments in C

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.

Page 5: Module 04 Command Line arguments in C

Example

int main(int argc, char *argv[ ]) {

int i;

for (i = 1; i < argc; i++) printf("%s ", argv[i]);

printf("\n");

return 0;

}

Page 6: Module 04 Command Line arguments in C

Thank you

This presentation is created using LibreOffice Impress 3.6.2.2