Search code examples
cansi-c

Why the first argument of argv is 1 not 0


I have some parameters to the program work correctly. This arguments shoud be, MAX_NUM, x, y.

When catching the parameters of the char input list, I currently obtain MAX_NUM using the argument 1 instead of 0.

Ex:

int main (int argc, char *argv[]) {
    int MAX_NUM = atoi(argv[0]);
    int x = atoi(argv[1]);
    int t = atoi(argv[2]);
    printf("MAX_NUM %d\n", atoi(argv[0]));
....

Priting argv[1] i get the MAX_NUM correctly, when printing the first argument gets 0.

Why C init the char input list array on 1 instead of 0 or the program name?


Solution

  • In simple terms, the value of argv[0] is the name of the program to execute. C kinda reserves the argv[0] index for this purpose. To get into some nitty-gritty details of how C and your operating system start to work with each other - check out this post in Stack Overflow.