Proper Declaration and Return Values of main() Function in C and C++

Nov 19, 2025 · Programming · 17 views · 7.8

Keywords: main function | C language | C++ | program entry point | return value specification

Abstract: This technical article provides an in-depth analysis of the correct declaration methods, return value semantics, and parameter usage specifications for the main() function in C and C++ programming languages. By examining standards such as C11 and C++11, it explains why int main() should be used instead of void main(), and compares different parameter forms. The article also discusses the meanings of return values 0, EXIT_SUCCESS, and EXIT_FAILURE, along with default behaviors when omitting return statements in C99/C11 and C++. Finally, it covers implementation-defined extensions and considerations for recursive calls to main().

Basic Specifications of the main() Function

In C and C++ programming, the main() function serves as the program entry point, and its declaration directly affects the standardization and portability of the program. According to language standards, the main() function should return an int type value to report the program's exit status to the operating system or calling environment.

Return Value Meanings and Standards

The return value of the main() function indicates the program's exit status: returning 0 signifies normal termination, while non-zero values indicate abnormal termination. Although the specific meanings of non-zero values are not standardized, they are typically used to indicate different error types. In C, the macros EXIT_SUCCESS and EXIT_FAILURE defined in the <stdlib.h> header can be used to enhance code readability.

In C99 and later C standards, as well as in C++ standards, if the main() function reaches the closing brace without an explicit return statement, the compiler automatically inserts return 0;. This feature simplifies code writing, but in the C90 standard, omitting the return statement results in undefined behavior.

Standard Declaration Forms

The C and C++ standards explicitly define valid signature forms for the main() function. In C, the following two forms are recommended:

int main(void)
{
    // Program code
    return 0;
}

Or the parameterized form:

int main(int argc, char *argv[])
{
    // Use argc and argv to process command-line arguments
    return 0;
}

Here, char *argv[] can be equivalently written as char **argv. In C++, the standard similarly requires the return type to be int and supports the same parameter forms.

Issues with void main()

void main() is explicitly prohibited by the C++ standard and does not conform to standard specifications in C. Although some compilers (such as Microsoft Visual Studio) may support this form, it prevents the program from returning an exit status to the environment, thereby affecting portability and integration with other tools.

Parameter Constraints and Usage

When using the parameterized form of main(), the following constraints must be observed:

These parameters can be modified during program execution and retain their last stored values until program termination.

Implementation-Defined Extensions

Some implementations support extended forms of the main() function, such as the three-parameter form in Unix systems:

int main(int argc, char **argv, char **envp)

where envp points to an array of environment variables. While this form may be available in certain environments, it makes the program implementation-defined rather than strictly conforming to standards.

Considerations for Recursive Calls

In C, the standard permits recursive calls to the main() function, but this is generally not considered good programming practice. In C++, the standard explicitly prohibits calling the main() function from within the program. Recursive calls may lead to stack overflow and other difficult-to-debug issues.

Efficiency Considerations

Regarding the efficiency of main() function definitions, it is essentially irrelevant. Since the main() function is called only once during the program's lifecycle (marking program startup and termination), the impact of its definition on overall program performance is negligible.

Best Practice Recommendations

Based on standard specifications and practical development experience, the following recommendations are advised:

  1. Always use int main(void) or int main(int argc, char *argv[]) forms
  2. Return 0 or EXIT_SUCCESS when explicitly indicating success
  3. Return non-zero values or EXIT_FAILURE when indicating failure
  4. Avoid using implementation-specific extended forms unless justified
  5. Strictly adhere to the prohibition of calling main() in C++

Following these guidelines ensures code portability across different compilers and platforms while maintaining good integration with standard tools and scripts.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.