3

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

Community
  • 1
  • 1
user1299661
  • 183
  • 2
  • 3
  • 11

3 Answers3

8

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
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • i just wonder how i configure for codeblock? – Mustafa Ekici Aug 22 '12 at 13:49
  • @mekici: Sorry, I cannot help you in that, as I've no experience with CodeBlock. Google may help you, though. :-) – Nawaz Aug 22 '12 at 14:48
  • There're too many. I used Microsoft Visual Studio 2010, and sometime [Vim (graphical one)](http://www.vim.org/download.php) with MinGW (on Windows, of course). You can also see [Eclipse](http://www.eclipse.org/downloads/) – Nawaz Aug 22 '12 at 15:07
  • 1
    ok I find it you can edit this your answer, for codeblock, Settings=>Compiler Settings => check checkbox which write -pedantic – Mustafa Ekici Aug 22 '12 at 15:45
1
cout<<"hello"<<endl;
**main();**
return 0;

The second line main() will cause an infinite recursive loop with the main() function calling itself again and again, this in turn will cause no path in your code return value.

Since not all paths are returning values, no C compiler will compile this. Forget C, even C# compiler halts when it finds that all paths are not returning a value, though the code is legible.

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
  • The strange thing is that it does not even create the sensible recursive loop. – user1299661 Aug 11 '12 at 18:23
  • 1
    Thats because you used the GCC compiler which has its own rules, or perhaps it wants you, the programmer to take care of these things. Try with a Microsoft compiler - you won't be able to do it. – Prahlad Yeri Aug 11 '12 at 18:27
  • 1
    Update: Just out of curiosity, I tried compiling your code with both MinGW and the Microsoft CL compilers. As expected, MinGW compiled it, but cl threw this error: d:\source\c\test.cpp(9) : warning C4717: 'main' : recursive on all control paths, function will cause runtime stack overflow Microsoft (R) Incremental Linker Version 9.00.30729.01 Copyright (C) Microsoft Corporation. All rights reserved. /out:test.exe test.obj – Prahlad Yeri Aug 11 '12 at 18:32
  • It seems to me like the Microsoft Compiler is best to work with. – user1299661 Aug 11 '12 at 18:39
  • I think its a bit unfair, basing your opinion on just this one aspect. Remember that gcc is free+open-source and also cross-compiles on linux platform. Moreover, If you have written good ANSI C/C++ code, show me a compiler that refuses to compile that! – Prahlad Yeri Aug 11 '12 at 18:42
0

You can call any function under main function.main is also a function which is trigger in runtime by complier. Yes it is illegal act but you can call main function under main.To call main function under main cause recessive and run infinitely time. In VS2008 you will get a warning to call main function but the program runs without any problem .

Sad Al Abdullah
  • 379
  • 2
  • 8
  • So why would this compiler or IDE not warn me of such imminent danger? Even beyond that, why would it completely omit the line of code calling the main function to product the output of : "hello"? – user1299661 Aug 11 '12 at 18:21