Brought to you by molecularsciences.org.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
This publication may not be redistributed without this notice.

Reading command-line input in C

Following a program which echos whatever you type in the command line.

#include main(int argc, char *argv[]) { int i; for(i = 0; i < argc; i++) { printf("arg %d: %s\n", i, argv[i]); } return 0; }

argc is the count of the number of command-line arguments. argv is array of words. Each word is a string. So if you type:

myprogram.exe my input

argv[] would fill as follows:

argv[0] = myprogram.exe
argv[1] = my
argv[2] = input

The terms argc and argv are used by convention.