50 likes | 238 Views
Command Line Arguments. L.O. Hall. Two arguments to main. The first is an integer which is the number of items on the command line. It includes the program name. So, >= 1. The second is a two-dimensional array of characters.
E N D
Command Line Arguments L.O. Hall
Two arguments to main. • The first is an integer which is the number of items on the command line. It includes the program name. So, >= 1. • The second is a two-dimensional array of characters. • Each row contains a character string of the value on the command line at that position.
So, • myprog 3 this 4 Would have four arguments. The first row of the character array would contain “myprog”, the second row would contain “3”, the third row would contain “this”, and the fourth row would contain “4”. • The double quotes indicate these are character strings that are terminated by ‘\0’or NULL.
/* Two arguments to main. The first is an integer which is the number of items on the command line. It includes the program name. We print the two arguments as strings. */ int main(int argc, char *argv[]) { int request; if (argc != 2) { printf("Error need 2 arguments \n"); exit(1); } printf(“First argument %s \nSecond argument %s \n”, argv[0], argv[1]); }
249 grad: gcc command.c 250 grad: a.out 1 First argument a.out Second argument 1 251 grad: a.out one First argument a.out Second argument one 252 grad: a.out anything First argument a.out Second argument anything 253 grad: a.out 1 2 Error need 2 arguments 254 grad: gcc -omyprog command.c 255 grad: myprog forty-five First argument myprog Second argument forty-five 256 grad: