# Int vs void or other type of main functions in C/C++

• C/++/#
I read somewhere that this problem is quite old, and that some new compilers don't care about the main function being declared as void or other types anymore. But i am still quite bothered by it.

I made a little research about this and read that the main must have a return type and it has to be int. One reason was because it must return a value so that the OS can check if the whole program have been working properly. Such that if declared as other datatype it would cause datatype conflict - sometimes leading to a crash. And it is so important that it is part of the C-99 standard. But i have been playing around with c++ a while and have a little different reasoning.

In general, the return statement have many uses inside a function. One use of the return statement, is to give back a value requested by another function; where the return type is the same as the as the function which contains the return statement. For example:
int funct1() {
int a = 5;
return a; //<-- returns integer
}
char funct2() {
int b = funct1(); //<--if funct1 was not declared int then it would conflict with datatype of b
int c = 5;
char d = 0;
if (b == c) d = 90;
return d; //<-- returns the value of d in character datatype
}

Another use of the return statement is to indicate the termination of the function. For example:
int funct0() {
int c = 20;
return c; //<--terminates the function if previous statements have been successfully executed
if (c == 20) return 15; //<--will never be executed since it always terminate in the previous statement.
}

As you can see from funct0, a return statement will only be executed if the previous statements before it being ran properly. So "return 0;" in the main function tells the OS that he program ran properly? But then it will be true only if "return 0;" was not used as a program termination before the last return statement.

In addition, i don't see what is so bad about the main function being declared void and have a return statement as a program termination with no return value. Also, it is no fun for a OS to display a message of "your program runned successfully" everytime you run one- which it is why they don't show such message. Inclusively, the program would not have to bother to send the OS the value of 0 in integer of 4 bytes in most of the current 32bits compilers.

Rather, i see that the "return 0;" inside a main function is a terminated properly message for the OS while a void return is a terminated without warning. And then and again, i don't see much of a difference having the main function declared int, void or other datatypes - other than sending some type of values to the OS after the program termination. (except for void which doesn't return anything)

Then, why some people still argue that a program must have a int return type? The only one i can see is that; it is because it is part of the standard.

Related Programming and Computer Science News on Phys.org
Hurkyl
Staff Emeritus
Gold Member
There is nothing to argue -- the standard says main must return int, therefore in any standard-conforming C program, main must return int.

What happens with the value returned by main is beyond the purvue of the C standard. One typical usage is that the value is passed back to the context where the program was invoked -- for example, in a shell script or batch file. That 0 represents "successful completion" is simply a convention.

chroot
Staff Emeritus
Gold Member
Some operating systems, like Windows, do not use the return value at all. Other operating systems, like most Unix-like operating systems, may use the return value if your program is called automatically by a script or build environment of some sort. There are some situations where the return value is an incredibly handy thing to have, which is why it's part of the standard.

It's really a pretty silly thing to worry so much about either way. Just return zero if you don't know what else to return.

- Warren

Yes, I guess you are right. Any standard based program should follow the standard and that is it.

But still great! I mean i learned a many things after posting this. One thing include the shell script being the substitute of batch files in Unix and that in Unix they still use the 0 return.

Thanks guys.

P.S.: Here is a interesting article of the word "purvue" that you used Hurkyl.
To tell the truth, i am multilingual and English is my third language. I kinda got the idea of the meaning of the word by the sentence but still wasn't sure. I couldn't find the meaning of the word in any dictionary until i stumbled into the article. And in this article says that the word is spelled "purvue" while i could only find it spelled "purview" in the dictionary. It is not like i am recommending not to use the word like the article; i was just shocked that my English was that poor and relieved after i read the article.

Last edited by a moderator:
rcgldr
Homework Helper
Some operating systems, like Windows, do not use the return value at all.
If a Windows program is invoked from a batch file, then the return value can be tested with "if errorlevel ..." or using the environment variable %errorlevel%. In Windows XP, open a DOS console window and enter "help if" to see the various options. This has always worked for me in the past, and I just reconfirmed this with Visual Studio 2005.

chroot
Staff Emeritus
Gold Member
You're correct, Jeff. I meant that the return value is ignored when the program is started from the Windows Explorer (the graphical shell).

- Warren

If a Windows program is invoked from a batch file, then the return value can be tested with "if errorlevel ..." or using the environment variable %errorlevel%. In Windows XP, open a DOS console window and enter "help if" to see the various options. This has always worked for me in the past, and I just reconfirmed this with Visual Studio 2005.
Can you write a complete step with examples? So that i could test it too?

Thanks.

rcgldr
Homework Helper
Can you write a complete step with examples? So that i could test it too?
Assume you have a program called SampleWindowsProgram.exe. Create a batch file with the following lines:

Code:
SampleWindowsProgram
@echo SampleWindowsProgram returned a %errorlevel%
Then open a dos console window and run the batch file.

return value is ignored when the program is started from the Windows Explorer
You could create a shortcut to run a batch file (put a "pause" at the end to prevent the console window from closing at completion).

Last edited:
Hurkyl
Staff Emeritus
Gold Member
P.S.: Here is a interesting article of the word "purvue" that you used Hurkyl.
I knew the spelling looked wrong.

Great! Thanks Jeff Reid, i have been experimenting a lot with MSVC2005. I have gotten the following results:
-It seems that I can only declare the main in int or void - the compiler will give me a error for other datatypes.
-And very interestingly, if no return is specified or if the return is void, it would say that it returned 0.
-Also, it will only receive the first int return and terminate the program skipping the rest - including other returns values.

KTC
Great! Thanks Jeff Reid, i have been experimenting a lot with MSVC2005. I have gotten the following results:
-It seems that I can only declare the main in int or void - the compiler will give me a error for other datatypes.
-And very interestingly, if no return is specified or if the return is void, it would say that it returned 0.
-Also, it will only receive the first int return and terminate the program skipping the rest - including other returns values.
The C++ standard (don't know about C) states main() shall automatically return 0 if main() completes and no return value have been explicitly specified by the user. Note that this rule only applies to main() and not any other function.

Once you return, you're leaving a function, everything else afterwards might as well not be there. This is a general feature of functions, nothing specific to main().

The C++ standard (don't know about C) states main() shall automatically return 0 if main() completes and no return value have been explicitly specified by the user. Note that this rule only applies to main() and not any other function.

Once you return, you're leaving a function, everything else afterwards might as well not be there. This is a general feature of functions, nothing specific to main().
I see, so C++ have a different standard from C.

For your second paragraph, it is true that return is just a feature of functions and not main. I was just saying that return 0 is not a very good tester if the program ran properly, specially if there are more than one return statements reside within the main function.

KTC
Well yes, C++ (ISO/IEC 14882) operate on a different international standard than C (ISO/IEC 9899). But no, in this case (I've got off my lazy backside and checked) C return 0 automatically as well upon reaching the } that terminates the main function without having encountered an explicit return.

I was just saying that return 0 is not a very good tester if the program ran properly, specially if there are more than one return statements reside within the main function.
Of course the "return 0" convention is a good tester that the program completed normally, unless you do not write your program to report its outcome based on this convention. Take something like this for example:
Code:
int main() {
FILE *f = fopen("myfile.dat", "r");
if (f) {
/* Use the file, then report success to the OS. */
/* ... */
fclose(f);
return 0;
}
/* Failed to open "myfile.dat", report this error to the OS. */
return 1;
}
You are the one who writes the function. You should return 0 if it did what you wanted and some other value otherwise. If you pick a different convention then the OS may not handle your program's result the way you intend. There is nothing mystical here, it's a simple convention that works.