Meaning of Arguments in Main Function

Hi,

I have a question about a main function with the following arguments:

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

Let's say the above line comes from a source code named exe1. If I compile this source code, what role do the arguments argc and argv have when running the code?
They allow you to pass arguments to the program, typically from the command line. E.g. if you run:

 
myapp --argument --arg=value file.txt


then argv looks like this:

1
2
3
4
5
argv[0]: "myapp"
argv[1]: "--argument"
argv[2]: "--arg=value"
argv[3]: "file.txt"
argv[4]: NULL


argc would be 4 (the number of arguments passed)
Last edited on
Topic archived. No new replies allowed.