The main Function

image with no caption

Every program must have a function in the global namespace called main, which is the main program. This function must return type int. The C++ environment calls main; your program must never call main. The main function cannot be declared inline or static. It can be called with no arguments or with two arguments:

int main(  )

or:

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

The argc parameter is the number of command-line arguments, and argv is an array of pointers to the command-line arguments, which are null-terminated character strings. By definition, argv[argc] is a null pointer. The first element of the array (argv[0]) is the program name or an empty string.

Tip

image with no caption

An implementation is required to support at least two forms of main: one that takes no parameters, and one that takes the two parameters for command-line arguments. An implementation can support other forms of main. The standard recommends that the first two parameters should be the same as they are in the standard, and that additional parameters follow them.

image with no caption

Static objects at namespace scope can have constant or dynamic initial values, those of POD type with constant values are initialized by constant data before the program ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.