Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:12:10+00:00 2026-05-13T13:12:10+00:00

Can anybody tell what’s the problem of the code below? int main () {

  • 0

Can anybody tell what’s the problem of the code below?

int main () { 
    return main(); 
}

I tested, it compiles correctly. It’s running forever. Anymore trick behind the scene?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:12:11+00:00Added an answer on May 13, 2026 at 1:12 pm

    TLDR: Calling main results in undefined behavior.


    There seems to be confusion about the terminology used in the standard, and the implications that has for the programmer and compiler.

    Firstly, the standard alone determines everything about the C++ language. If your particular version of a particular compiler allows some particular action, that has no bearing on whether or not that action is legal. For the remainder of the post, I’m referring to the ISO03 standard.

    So to quote once again, the standard says in §3.6.1.3:

    The function main shall not be used within a program.

    Additionally, §3.2 defines “used” as:

    An object or non-overloaded function is used if its name appears in a potentially-evaluated expression.

    This means that once the program begins executing, main should never be entered again. That means programmers cannot call main, that means the compiler cannot insert another call to main (why it would, who knows), you cannot take the address of main and call that, etc. You cannot even have the potential of calling main.

    The only call to main should be by the run-time library the program is running on; all other calls invoke undefined behavior. (Which means anything could happen!)


    Now onto compiler behavior:

    A diagnosable rule is defined as (§1.4.1):

    The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”

    In our case, §3.6.1.3 defines a diagnosable rule. Here’s what compilers should do according to §1.4.2:

    — If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute3) that program.
    — If a program contains a violation of any diagnosable rule, a conforming implementation shall issue at least one diagnostic message, except that
    — If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.

    So compilers are not required to enforce rules. All compilers have to do is take well-formed programs (§1.3.14) and turn them into an executable program. A compiler is free to warn, error, etc. however it likes, as long as it does not conflict with the language. It is required to display a message in our particular case, according to the second clause.

    For this particular problem, on gcc the -pedantic option will warn about the illegality of calling main within the program. Visual Studio will not warn about calling main, but on any warning level (greater than 0) it will warn about the recursive nature of the program.


    What does all this mean in terms of the answers you should expect? It means it’s completely meaningless to try and define with certainty what the code snippet posted will do. Calling main results in undefined behavior, and trying to define undefined behavior is obviously a lost cause. The only honest answer anyone can give to “what happens when I call main?” is “Anything.”

    I hope this clears things up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anybody tell me why is the following code not working? <script type=text/javascript src=../../Scripts/jquery.js></script>
Can anybody tell me how to make this C code work in C++ please?
Can anybody tell me How to store and return List of Strings. I am
can anybody tell me where can i find some tutorials/ sample code on how
Can anybody tell me why the following code doesn't work? I don't get any
can anybody tell me what is wrong with this code, i am getting the
Can anybody tell me, why PHP_EOL is not working on my Mac? Code: echo
Can anybody tell me why this code: SELECT t.prob FROM (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob
Can anybody tell me how to parse and edit HTML in PHP?
Can anybody tell me how to search for available iPhone/iPod devices on a network

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.