Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling the main function in C++ is ignored using CodeBlocks [duplicate]

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

like image 359
user1299661 Avatar asked Oct 19 '25 16:10

user1299661


1 Answers

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
like image 51
Nawaz Avatar answered Oct 22 '25 06:10

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!