Home  >  Article  >  Backend Development  >  Is Recursion into `main()` Legal in C ?

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

DDD
DDDOriginal
2024-11-18 20:13:02537browse

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

Recursivity into main() in C : Legal or Not?

A common misconception regarding C is the legality of recursion into the main() function. While it may seem intuitive that such a practice is prohibited, the compiler often does not object to such code. To clarify this issue, let's delve into the intricacies of the C standard.

According to the C standard (3.6.1/3), recursion into main() is not permitted. The standard explicitly states that "The function main shall not be used within a program." In this context, "used" implies that the function is referenced in a potentially-evaluated expression.

Therefore, the following code is in violation of the C standard:

int main()
{
    main();
}

Despite this prohibition, some compilers may not issue an error when encountering such code. This may lead developers to believe that recursion into main() is legal, but it is important to note that this behavior is erroneous. Reliance on compiler acceptance should not be substituted for adherence to the standard.

In conclusion, according to the C standard, recursion into main() is illegal. While compilers may not always enforce this rule, developers should be aware of the standard's requirement and avoid this practice in their code.

The above is the detailed content of Is Recursion into `main()` Legal 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