+ 1

Return value in main function

main function is always int therefore it has to return positive integer value only but execution takes place even if we do not return any value or use some float value (return 2.1) or signed (return -1) . how?

21st Jun 2017, 9:33 PM
Rishabh Singh Rajput
Rishabh Singh Rajput - avatar
2 Respuestas
+ 13
The return value from your main function is an indicator to the operating system of how the program exited. You can potentially make use of that value in further work (a pipeline of programs, for instance).
22nd Jun 2017, 12:13 AM
Jim
Jim - avatar
+ 3
int is signed, not unsigned. You can return negative values. It wouldnt cause an error anyways if it was unsigned, if an unsigned variable is given a negative value it just overflows to the max value, much like a clock when it goes backwards from 1. (1 - 2) becomes 11 on a clock. If you return a float like 2.2(it'd actually be a double in this case), your compiler will ignore the decimal point and just return 2
21st Jun 2017, 10:13 PM
aklex
aklex - avatar