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

In summary, the author argues that the main function in a C program should have a return type of int, because it is part of the standard and is a way to give back a value requested by another function. He also argues that the main function should be declared void, as a way to indicate the termination of the function.
  • #1
cshum00
215
0
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.
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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
 
  • #4
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.
http://www.businesswritingblog.com/business_writing/2006/03/whose_purview_i.html"
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:
  • #5
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.
 
  • #6
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
 
  • #7
Jeff Reid said:
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.
 
  • #8
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:
  • #9
cshum00 said:
P.S.: Here is a interesting article of the word "purvue" that you used Hurkyl.
I knew the spelling looked wrong. :frown:
 
  • #10
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.
 
  • #11
cshum00 said:
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().
 
  • #12
KTC said:
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.
 
  • #13
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.
 
  • #14
cshum00 said:
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.
 

What is the difference between "int main()" and "void main()" in C/C++?

The main function in C/C++ is the entry point of a program, where the execution of code begins. The difference between "int main()" and "void main()" is the return type of the function. "int main()" indicates that the function will return an integer value to the operating system, while "void main()" does not return any value. However, both can be used to execute the same code and are considered valid by most compilers.

Why is "int main()" the standard in most C/C++ programs?

"int main()" is the standard for the main function in most C/C++ programs because it allows the program to return an exit status to the operating system upon completion. This exit status can be used to indicate the success or failure of the program's execution.

Can "void main()" be used in place of "int main()" in all C/C++ programs?

No, "void main()" cannot be used in place of "int main()" in all C/C++ programs. Some compilers may allow it, but it is not considered good practice. The C++ standard states that the main function must return an integer value, and using "void main()" goes against this standard.

What happens if a program does not have a main function?

If a program does not have a main function, it will not be able to execute. The main function is the starting point of a program, and without it, the operating system will not know where to begin executing the code. The compiler will likely throw an error if it cannot find a main function in the program.

Can the return type of "int main()" be something other than an integer?

No, the return type of "int main()" must be an integer. The C++ standard states that the main function must return an integer value, and using a different return type goes against this standard. Some compilers may allow it, but it is not considered good practice.

Similar threads

  • Programming and Computer Science
Replies
2
Views
686
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
920
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
Back
Top