trying to condense my notes: int main(int argc, char *argv[])

Hello, I'm trying to condense my notes into layman's terms. I'm having difficulty with int main(int argc, char *argv[]). If you can assist me with this, I would greatly appreciate your timed effort.

So far to my understanding int main() or int main(void), which are the same operate as listed:

1
2
3
4
int main()
{
return 0;
}

The parentheses (()) followed by the word main tells the compiler that there is a function named main, and that the function returns an integer (whole number), hence int.

Optionally, these parentheses may enclose a list of parameters within them.

The main function is the point by where all C++ programs start their execution, independently of its location within the source code. For that same reason, it is essential that all C++ programs have a main function.

The curly braces ({}) signal the beginning and end of the functions and other code blocks.

 
return 0;

This is a return statement that indicates that the program ended successfully. This ends execution of the executable program in the main function.

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


The function main is declared with the parameters, int argc, char *argv[].

Although any name can be given to these parameters, they are usually referred to as argc and argv.

The first parameter, argc (argument count), has type int and indicates how many arguments were entered on the command line.

The second parameter, argv (argument vector), has type array of pointers to char array objects. char array objects are null-terminated strings.

The value of argc indicates the number of pointers in the array argv. If a program name is available, the first element in argv points to a character array that contains the program name or the invocation name of the program that is being run. If the name cannot be determined, the first element in argv points to a null character.

This name is counted as one of the arguments to the function main. For example, if only the program name is entered on the command line, argc has a value of 1 and argv[0] points to the program name.

Regardless of the number of arguments entered on the command line, argv[argc] always contains NULL.


return doesn't indicate that the program is exiting, it indicates that the function is exiting. In this case, it indicates that main() is exiting. It just so happens that when main exits, so does the program.


If the name cannot be determined, the first element in argv points to a null character.


Is this true? I find it hard to believe.

argv[] is just a snapshot of whatever was put on the command line to launch the program. It's impossible to launch the program without some command, so I don't see how argv[0] could ever be an empty string.

For example, if you run the program with the following:

myprogram.exe -someoption myfile.txt

then you have the following:
1
2
3
4
argc == 3
argv[0] == "myprogram.exe"
argv[1] == "-someoption"
argv[2] == "myfile.txt"



Regardless of the number of arguments entered on the command line, argv[argc] always contains NULL.


I never knew this... I just assumed argv[argc] was out of bounds. But I wouldn't be surprised if I was wrong.
Thank you for correcting my return statement.

This is where I literally got my notes for int main(int argc, char *argv[])

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc07mainarg.htm

I also found this tutorial that helped me understand it indepth:

http://www.phon.ucl.ac.uk/courses/spsci/abc/lesson11.htm

But I think I'm jumping the gun, since I'm a few days old in my studies.
Last edited on
Topic archived. No new replies allowed.