1 / 5

Command Line Arguments

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.

natara
Download Presentation

Command Line Arguments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Command Line Arguments L.O. Hall

  2. 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.

  3. 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.

  4. /* 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]); }

  5. 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:

More Related