Home >Backend Development >C++ >Is Recursion into `main()` Allowed in C ?

Is Recursion into `main()` Allowed in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-28 19:16:15138browse

Is Recursion into `main()` Allowed in C  ?

Recursion into main() in C : Is it Allowed?

Despite the notion that C prohibits recursion into the main(), there are instances where code like the following compiles without errors using g :

int main()
{
    main();
}

This raises the question: does the C standard permit such recursion?

Answer:

The C standard explicitly forbids recursion into main() in section 3.6.1/3, which states that "The function main shall not be used within a program." In this context, "used" refers to situations where "its name appears in a potentially-evaluated expression."

Therefore, recursive calls to main() are not allowed in C programs. The compiler's ability to compile the provided code without errors is likely due to its implementation-specific optimizations or a loophole in its interpretation of the standard. However, it is not recommended to rely on such behavior as it goes against the explicit prohibition in the language specification.

The above is the detailed content of Is Recursion into `main()` Allowed in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn