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

I went through this and this

but the question I ask here is why is 0 considered a Success ?

We always associate 0 with false , right?

+4
source share
5 answers

Because there are more cases of failure than success cases.

Usually we only succeed in one reason (because we are successful :)), but there are many reasons why we could fail. Thus, 0 means success, and everything else means failure, and the value can be used to report the reason.

For the functions in your code, this is different because you are the one who points out the interface, and so you can just use bool if that's enough. For main there is one fixed interface for returns, and there may be programs that simply report completion / failure, and others that require more accurate reporting of errors. To satisfy them all, we will have many mistakes.

+20
source

I need to talk a little with Johannes answer. True 0 is used for success because there is only 1 successful result, while there can be many unsuccessful results. But my experience is that return codes have less to do with failure reasons than failure levels.

In the days of batch programming, there usually were conventions for return codes that allowed some automation of the overall flow of execution. Thus, return code 4 may be a warning, but the next work may continue; 8 may mean that the work flow should stop; 12 may mean something catastrophic and the fire department should be notified.

Similarly, batches will allocate some range of return codes so that the overall packet stream can branch out. If, for example, the updater returned XX, the package may skip the backup step because nothing has changed.

Return codes as the causes of the failure are not all that useful, of course, not as many as log files, kernel dumps, console alerts, and much more. I have never seen a system that returns XX, because "such and such a file was not found", for example.

+5
source

Usually the return values ​​for any given program, as a rule, are a list (enumeration) of possible values, such as Success or specific errors. As a β€œlist”, this list usually tends to start at 0 and count up. (Aside, this is partly due to the fact that the Microsoft error code is 0 ERROR_SUCCESS ).

Globally, Success tends to be one of the only return values ​​that almost any program would have to return. Even if the program has several different error values, Success tends to be a common necessity and as such gives the most common position in the list of return values.

This is simply the easiest way to resolve the most commonly used default return value. It is completely separate from the idea of boolean .

+3
source

Here's the agreement I'm used to from different companies (although this obviously varies from place to place):

  • A negative number indicates an error. A negative number indicates (hopefully) the type of error.
  • Zero indicates success (overall success)
  • A positive number indicates the type of success (in some business situations, there are various things that cause a successful event .... this indicates what successful event has occurred).
+1
source

I also found this confusing when I started programming. I solved this in my mind by saying: 0 means no problems.

0
source

Source: https://habr.com/ru/post/1303001/


All Articles