If we have any return type then we write int main().
My question is that can we write float or character in place of int? if yes justify.
Question asked by visitor anjali
If we have any return type then we write int main().
My question is that can we write float or character in place of int? if yes justify.
Question asked by visitor anjali
I think main() should return only int type value.
Lack of WILL POWER has caused more failure than
lack of INTELLIGENCE or ABILITY.
-sutnarcha-
main can return only two type of values according to me one is of void type and one is of int type.
The c & c++ standard say that the return type of the main() function must be int. Otherwise the behavior is undefined. Some good compilers(like gcc) even show an error msg when u use void or float main while some prehistoric compilers(like tc or tcc) dont.
Hi...
Do you know main() returns the value to whom? Who calls the main() at the beginning of execution of a program, that is the OS. The main() returns two values only, either 1 or 0 via int variable type to OS to notify the program execution successful or not. So it should be int type only. But in case of an argument anybody can say that it can return the same value via Float.... in that case the answer is here the binding goes between the main() & the OS. So it should maintain its strict signature.
I'm not sure whether a conventional C/C++ compiler shows error if we use float instead of int or not, but it seems to me it's quite natural to accept float. Because after all compiler TC/TCC can provide so many strict parameters.....that's why they're more or less obsolete now.
One more important point....we can easily refer Void instead of int because this means the main() wouldn't reply anything to the OS!! It's totally legal because it seems main() takes the total responsibilty about the success/failure of the execution of that program.
Thanks,
Kuntal
The main() returns two values only, either 1 or 0 ::
The main can return any integer value and any value other than 0(EXIT_SUCCESS actually) indicates that the program's execution was a failure. The value returned can be used to take necessary actions using a shell script(in unix environment).
...because it seems main() takes the total responsibilty about the success/failure of the execution of that program...
Main() implicitly returns an EXIT_FAILURE only in cases where the program terminates unexpectedly. What if the user wants to return value 2 for an illegal input so that he can use it in his shell scripts? In such cases he must return the value all by himself. gcc actually makes it mandatory for the return value to be of int type to avoid any unexpected behavior.