C-programming board game [duplicate]

The main function takes two arguments, an integer usually called argc and an array of string pointers usually called argv. main is so usually declared as:

int main(int argc, char *argv[])

The argc variable is the number of arguments passed to your program.

The argv variable is contains the actual arguments pass to your program.

The argc variable is at least equal to 1, as the actual program name is always passed as an argument, and is in argv[0].

If you want two arguments, then you first have to make sure that argc is at least equal to 3. The first argument to your program is then stored as a string in argv[1], and the second is in argv[2].


I recommend you experiment with a program such as this:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("argc = %d\n", argc);

    for (int i = 0; i < argc; i++)
        printf("argv[%d] = \"%s\"\n", i, argv[i]);

    return 0;
}

And finally (and unrelated to your problem but interesting none the less) is the fact that the size of the argv array is actually one larger than most people expect. The size of the argv array is actually argc + 1 and so can be indexed from argv[0] (the program name) to argv[argc]. This last entry (argv[argc]) is always a NULL pointer.

Leave a Comment