- #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.
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.