Why did C++ developers made it mandatory to put return type of main as a 'int'

Hi all,
I checked some old C++ code online and there I saw the return type of main as a 'void' and nothing was being returned by the function.
In Modern C++, we put return type of main as an 'int' and at the last of the function we put (return 0, I tried replacing 0 by some other integers and it still worked). Is there any particular reason because of which C++ developers made this change from void to int returntype for main function.

Also, I wrote this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void printsomething(); //Prototype of printsomething()

int main()
{
    printsomething();
    return 23;
}


void printsomething(){
    cout << main() << endl;
}


If the main function really returns something, then the above code should have worked. But, it didn't. It was showing this error( I use codeblocks ).

error: ISO C++ forbids taking address of function '::main' [-pedantic]|

What's the meaning of the above error and why isn't this code working?

Thanks,
-Himansh
Last edited on
You did not output return value of main. Instead you are trying to output the address of main that is forbided by the Standard and the compiler reports about this.

The return value of main could be used in some batch process. Or your program could be called from another program that would check the return value.
Declaring main as void does not satisfy the C/C++ Standards.
You can have the main function return anything you want. It is so you can tell if the function runs correctly or not. If the function works correctly it will return that value. If it does not work correctly it will return a random number. You can also use return for error checking like
1
2
3
4
5
6
7
int main()
{
    int *a , b = 5;
    a = &b;
    if( a == NULL ) return( 1 );
    return( 0 );
}
Code finished with 0...or some crap like that


That will let you know if a is being assigned the value of b or if it is corrupt and still pointing to nothing.

And the main function doesn't work like that. That is like doing this
std::cout << while( a ) << std::endl;
or std::cout << std::vector<int> << std::endl;

Just because something is a function and has a return type doesn't mean you should output it.


PS you don't have to put a return type for int main it returns 0 by default with all the modern compilers AFAIK.

And the error is letting you know that you can't output int main because it is forbid. YOu must have int main function in all programs.
C was designed alongside Unix. The main() function returns a int value to the operating system. It has never been valid to declare main() as void, but several important older compilers allowed you to do it anyway.

There is another caveat: main() is not a normal function. Your compiler is giving you a bogus error message because you are trying to call main() directly -- something you cannot (and should not) do. You are actually in good hands because your compiler recognizes that you are trying to do something stupid and not letting it happen.

If you are using Windows, you can see the result by opening a Windows Console and running your program from that. See the result code by echoing the ERRORLEVEL environment variable.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!\n";
    return 23;
}

C:\Users\Himansh\Prog> a.exe
Hello world!
C:\Users\Himansh\Prog> echo %ERRORLEVEL%
23
C:\Users\Himansh\Prog> _


[edit] Oh, almost forgot. Don't return numbers that are any bigger than 255 on any system, and don't return anything negative. Only break this rule if you know something about the specific system your program is designed for.

Hope this helps.
Last edited on
Thank you all. I know that main is a special function and not any normal function, I asked this question just because I saw some old code with void returntype and it aroused my curiosity.

@giblit: You said that we can use main function for error checking. But how will we know what main has returned.
I mean that once main has returned something, it means that the program has been compiled completely, so how can we check if main has returned 1 or 0.

@Duoas:
Don't return numbers that are any bigger than 255 on any system, and don't return anything negative


Is there any particular reason for this? What will happen if I return something greater than 255 in main() function( or a negative no. ).


Thank you all, I am waiting for your replies :)
No it returns as soon as it hits a return statement. That will stop the function. It does not have to finish compiling the whole function.
Try compiling
1
2
3
4
5
6
7
8
int main()
{
    int a = 2;
    if( a == 2 ) return( 1 );
    a = 3;
    if( a == 3 ) return( 2 );
    return( 0 );
}


You will notice it returns 1 and not 2 or 0.

*forgot a keyword
Last edited on
@giblit:
Yes, I see that your code returned 1.
Also, I know that it will stop the function once it hits return.
What I meant was that how will we check what the main has returned so that we can perform different tasks for different return value.

1
2
3
4
5
6
7
8
int main()
{
    int a = 2;
    if( a == 2 ) return( 1 );
    a = 3;
    if( a == 3 ) return( 2 );
    return( 0 );
}


In the above code, main will return 1 if (a==2) ==> true
or main will return 2 if (a==3 && a!=2) ==> true
Otherwise, main will return 0.

But, suppose I want that if main returns 2, then do a specific task( for instance let just print "hello world" on the console window ).
So how will I check if main has returned 2;

More clarification to what I am asking
-----------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void check_return_value(); // Prototype

int main(){
    check_return_value();
    int number = 0;
    cout << "Enter a no. " << endl;
    cin >> number;

    if(number == 1){
        return 1;
    }else{
        return 0;
    }
}

void check_return_value(){
    if(main() == 1){
        cout << "Hello World" << endl;
    }else{
        cout << "main() did not return 1";
    }
}


I want that if main() returned 1, then print "hello world", otherwise print "main() did not return 1".
Now, how am I supposed to check if main() returned 1 or 0. Since, I cannot call main() within any other function to check if it has returned 1 or 0. (The above code will show error since I am calling main() within other function to check what it has returned, which is not possible)

I hope you understand what I am trying to say.

Thank you,
-Himansh
Himansh wrote:
how will we know what main has returned.

As Duoas' example shows, the shell calling the app will keep the return value for your convenience.

Himansh wrote:
Is there any particular reason for this? What will happen if I return something greater than 255 in main()

Compatibility. There might still be systems, which do use one byte for storing the return value.
Why in gods name would you want the function to return 2 then say hello world?

If you really want something to be equal to two then say hello world I guess you could do something like this
1
2
3
4
5
6
7
int main()
{
    int a = 2;
    if( a == 2 ) std::cout << "Hello world" << std::endl;
    else std::cout << "Good bye world" << std::endl;
    return( 2 ); /*Returns two and outputs hello world*/
}


WHen I said you can check by errors is say for example if you ever have a pointer that points to nothing (NULL) you can have the main function return 1 then and if you ever have a variable equal -1 you can have the main function return two. You will then know what errors you have. You can also put different returns types at different areas to check if you have something wrong. I suppose you could also just use the assert macro and that would tell you exactly what is failing.
1
2
#include <cassert>
assert( a != 2 );
Oh, I see.
I thought that you were saying that we can check what main has returned and then perform different actions.

Thank you. I got it now :)
It is a design philosophy in the Unix system. Back when it was written, batch processing was a big thing.

In Unix (and now most other systems too), you can use a program's return value to direct the batch scripting. The value 0 typically means "program executed without error" and non-zero means "program executed with errors".

A common example is GCC linker, ld, which will return 0 if your program's pieces properly linked together into an executable, and it will return 1 if it did not.

Batch systems like make can then tell if compilation succeeded and act accordingly -- keep trying to compile stuff according to the instructions you gave it in the makefile, or stop and complain that something didn't work.


The 255/negative limit is because of restrictions some systems place on the return value. It isn't really meant to return anything but a flag of some kind; and there aren't that many things you can really signal about how your program terminated.

Glad you're having fun with this!


[small]Also, it must be a fairly new feature where other people responding get their posts placed in line when they began responding... has anyone else noticed that?
Hmm, It seems like I have a lot to learn from you guys. Keep up your good work of helping others. I like getting knowledge from you all. Thanks :)
Last edited on
Topic archived. No new replies allowed.