If I have an int main(void)
function in C, should it have a return 0
? If so, why?
int main(void) {
printf("Does this function require return value");
return 0; // is this correct?
}
If I have an int main(void)
function in C, should it have a return 0
? If so, why?
int main(void) {
printf("Does this function require return value");
return 0; // is this correct?
}
Yes it is. you can use the return value as a status.
Returning 0 usually indicates a success.
Here is an interesting article discussing the matter : http://www.eskimo.com/~scs/readings/voidmain.960823.html
The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these are equal to 0 and 1 respectively. A C program may also use the exit() function specifying the integer status or exit macro as the first parameter. http://en.wikipedia.org/wiki/Exit_status#C_language
Yes ,Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:
int main ( void )
int main ( int argc, char *argv[] )
Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
The first option is used when you do not require access to the command line arguments.
The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.
The return type of main() must always be an int, this allows a return code to be passed to the invoker.
Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.