0

I found a lot of C tutorials on the internet ending their main functions with return 0;, but I don't see David doing that. Can anyone explain to me please? I've only finished watching Week 1 lectures so forgive me if this is already answered in other lectures.

2

Typically and according to the standard, the return value of main should be of type int which obviously means that you should return an int value from main.

However, you don't really have to do that in C99/C11 as in these standards, if you do not explicitly return an int value from main, the value 0 is returned automatically.

From C11 standard:

5.1.2.2.3 Program termination
reaching the } that terminates the main function returns a value of 0

In earlier versions of C though, the result of not explicitly returning an int value from main caused the termination status to be undefined.

1

I am not sure which video he mentions this (at least, I hope he does) in the new videos, but somewhere in last years videos I caught the answer to this question.

Basically, C is smart enough that, should you neglect to return 0 by the end of your main function, it does it for you. So no, you don't need to write the command, but you can if you want the program to end at a particular line under the belief that "everything ended alright." If, however, you want to end a program saying that something went wrong, the convention is to instead return a non-zero number, which somewhere in your documentation for a more complex program you would record the meaning of.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .