1int // Specifies that type of variable the function returns.
2 // main() must return an integer
3main ( int argc, char **argv ) {
4 // code
5 return 0; // Indicates that everything went well.
6}
1#include <stdio.h>
2
3int main(int argc, char **argv) {
4 for (int i = 0; i < argc; ++i) {
5 printf("argv[%d]: %s\n", i, argv[i]);
6 }
7}
8
9/*
10 [birryree@lilun c_code]$ ./a.out hello there
11 argv[0]: ./a.out
12 argv[1]: hello
13 argv[2]: there
14*/
1You can use as your main function:
2int main(int argc, char **argv)
3
4So, if you entered to run your program:
5C:\myprogram myfile.txt
6
7argc will be 2
8argv[0] will be myprogram
9argv[1] will be myfile.txt
10
11To read the file:
12FILE *f = fopen(argv[1], "r");