c++codeblocksprogram-entry-point

Calling the main function in C++ is ignored using CodeBlocks


Possible Duplicate:
Can main function call itself in C++?

I decided to do a small test using CodeBlock IDE by calling the main function which should be an illegal act.

EX:

#include <iostream>
using namespace std;

int main()
{
  cout<<"hello"<<endl;
  main();
  return 0;
}

Strangely, in code blocks I was able to compile this mess. Does anyone know why?

Output: hello


Solution

  • As you said in your question itself that calling main() explicitly from your code is forbidden by the language specification. Only the runtime can call it.

    As you use GCC to compile your code (read your comment), the -pedantic option would give you appropriate diagnostic in the form of error or warning. So try this:

    g++ program.cpp -pedantic