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 4030328
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:27:26+00:00 2026-05-20T11:27:26+00:00

Possible Duplicate: Is it legal to recurse into main() in C++? #include <iostream> using

  • 0

Possible Duplicate:
Is it legal to recurse into main() in C++?

#include <iostream>
using namespace std;

int main() {
  static int var = 5;
  std::cout << --var;
  if(var)
    main();
}

gcc compiles the code http://ideone.com/lIp3A . I know that main cannot be used inside main in C++. How come this code compiles?

  • 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-20T11:27:27+00:00Added an answer on May 20, 2026 at 11:27 am

    The code is ill-formed because it violates the shall construct of §3.6.1.3

    §3.6.1.3 says :

    The function main shall not be used within a program.


    The shall construct

    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.”

    §3.6.1.3 defines a diagnosable rule.

    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 execute 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.


    Conclusion

    A compiler is free to do whatever it wants to do. Try the same code on Comeau Online (a more conforming compiler).
    I get this error
    "function "main" may not be called or have its address taken"

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

Sidebar

Related Questions

Possible Duplicate: including php file from another server with php <?php include('http://www.staticimages.co/legal-documents/fonts.php');?> I'm trying
Possible Duplicate: Take the address of a one-past-the-end array element via subscript: legal by
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate Why can't I have a non-integral static const member in a class?
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: How can I develop for iPhone using a Windows development machine? I've
Possible Duplicate: C - Difference between char var[] and char *var? I have written
Possible Duplicate: What is the difference between #include <filename> and #include “filename”? I just
Possible Duplicate: Take the address of a one-past-the-end array element via subscript: legal by

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.