Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understand argc and argv

Similar presentations


Presentation on theme: "Understand argc and argv"— Presentation transcript:

1 Understand argc and argv
Yung-Hsiang Lu Purdue University

2 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; }

3 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } an integer as indexes

4 %d means the value of an integer This line prints argc's value
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } %d means the value of an integer This line prints argc's value

5 argc is the number of arguments it is at least one
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } argc is the number of arguments it is at least one

6 the first argument is the program's name
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } the first argument is the program's name

7 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } ind is 0, 1, 2, ... argc - 1

8 and the value of the argument
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print the index and the value of the argument

9 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print an integer

10 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } print a string

11 #include <stdio. h> #include <stdlib
#include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { int ind; printf("argc = %d\n", argc); for (ind = 0; ind < argc; ind ++) printf("argv[%d] = %s\n", ind, argv[ind]); } return EXIT_SUCCESS; } ind is 0, 1, 2, ..., argc - 1 not 1, 2, ..., argc


Download ppt "Understand argc and argv"

Similar presentations


Ads by Google