What is the third parameter I sometimes see defined in the main function?

The third parameter to the main function is not standard or portable in C or C++, but in practice, most modern compiler implementations allow an optional third parameter to main. The data type of this third parameter is pointer to pointer to char (not just pointer to char), expressed as either

using pointer notation, or

using array notation.

Like argv, the envp parameter is a pointer to the first element of an array of pointers to null-terminated strings. But instead of containing command line arguments, elements of envp point to strings containing environment information. The information itself is environment-dependent and system-dependent. In most cases, the envp strings contain environment variable definitions, such as PROCESSOR_LEVEL=6.

Early versions of the C standard mentioned this third parameter under portability issues. The C11 standard mentions envp under Common extensions (J.5) Environment arguments (J.5.1):

In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program…

So, while it is mentioned in the standard as a common extension, envp is not a requirement of the implementation.

The C++17 standard doesn’t explicitly mention envp or any specifics about parameters beyond argv. It just leaves additional parameters up to the implementation, in section 6.6.1:

[ Note: It is recommended that any further (optional) parameters be added after argv. — end note ]

If you see a different name or data type in that third parameter to the main function, you might be observing someone’s misunderstanding of the data type and intent of the widely-adopted char *envp[] parameter. Or perhaps you’re working with a compiler implementation that has gone its own way from the rest of the pack, and if so, you would need to consult your specific compiler’s documentation for details on what that third parameter means.

Leave a Reply