0

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? 
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
Abulurd
  • 1,018
  • 4
  • 15
  • 31
  • possible duplicate of [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) http://stackoverflow.com/questions/12959866/what-does-main-return – phuclv Sep 17 '14 at 03:34
  • It is not a duplicate...I have already seen that post, it is not very clear to me at all. – Abulurd Sep 17 '14 at 03:38
  • 1
    Those questions already answer the question "why". Read again – phuclv Sep 17 '14 at 03:42
  • 1
    The answer provided is not clear as the one below. Perhaps, you should go troll someone else; I am only here to learn, not to confuse myself even more. – Abulurd Sep 17 '14 at 03:50
  • What's not clear? Did you read all the answers and some answers of the duplicated answers too? All the things in the below answers have been provided overthere – phuclv Sep 17 '14 at 04:03
  • Instead of a bare assertion that the other answer doesn't help, explain *why* it doesn't help. ("The question titled A explains X, and I understand this, but this leaves Y unexplained; my question is thus about Y"). Without such clarification (or narrowing, to be asking explicitly about content *not* explained elsewhere), this very much reads to me as a genuine duplicate. – Charles Duffy Jul 04 '17 at 15:45

2 Answers2

2

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

Bruno
  • 4,685
  • 7
  • 54
  • 105
1

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.

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59