Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we follow opposite conventions while returning from main()?

I have gone through this and this,

but the question I am asking here is that why is 0 considered a Success?

We always associate 0 with false, don't we?

like image 283
Lazer Avatar asked Nov 28 '22 12:11

Lazer


1 Answers

Because there are more fail cases than success cases.

Usually, there is only one reason we succeed (because we're successful :)), but there are a whole lot of reasons why we could fail. So 0 means success, and everything else means failure, and the value could be used to report the reason.

For functions in your code, this is different, because you are the one specifying the interface, and thus can just use a bool if it suffices. For main, there is one fixed interface for returns, and there may be programs that just report succeed/fail, but others that need more fine error reporting. To satisfy them all, we will have multiple error cases.

like image 183
Johannes Schaub - litb Avatar answered Dec 16 '22 10:12

Johannes Schaub - litb