1

Either I didn't searched well or this question is obvious.

In c, the main function returns what You want. But since Posix gives the example, 0 is for success and other values are for fail (mostly positive values in Linux).

Based on this assumption, I have seen a practive where all other functions in the code have the same behaviour.

  • Pros:
    • practical because you can return the result of your call directly as the result of your main
  • Cons:
    • 0 becomes OK, 1 becomes KO.

I am sure it is not good, but I have here 2 million lines based on that.

Is that common ? Does it have a name ?

Sandburg
  • 757
  • 15
  • 28
  • 1
    It's very common for posix functions to return -1 on error, and 0 or a positive number on success, yes, but I've never heard of a specific name for it. – Shawn Dec 17 '18 at 10:43
  • WinAPI, if returning success/failure, returns 1 on success and 0 on failure. They even aliased BOOL to int for that purpose... In C++ and in C, if using C99, I prefer `bool` to indicate success/error only (C: `'include `); if we want to indicate different error conditions, then I prefer an enum (so usually positive error values then). **But**: Best is having consistency over your code - so if you have the success == 0 pattern applied everywhere already, stay with! – Aconcagua Dec 17 '18 at 10:49
  • In my case, in the 2M lines, most of it is `cpp`, but it has inherited of this practice. So what if I use a `bool` instead of an `int` ? 0 is equivalent to true and 1 to false... tricky, insn't it ? – Sandburg Dec 17 '18 at 10:56
  • It depend of the utility of your fonction. If this function need to return an int the error return value will be anomal number, like for srcmp. But for normal usage it is 0 error, 1 normal. – Baptiste Gavalda Dec 17 '18 at 11:01

1 Answers1

1

Returning 0 for success is common. There is no specific name for such behavior.

This is what the C standard says:

7.22.4.4 The exit function
...
2. The exit function causes normal program termination to occur.
...
5. Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • 1
    for `main` yes, I am aware of that. It is a good convention. But for all the rest of the functions, not that sure. – Sandburg Dec 17 '18 at 10:59
  • 1
    `main` returns an `int`, but the rest of the functions are not limited to returning an `int` or integer types. So I do not think there can be a general mandate in this regard. If you are sure that all functions have to return only success or failure then a bool would suffice. – P.W Dec 17 '18 at 11:04
  • 1
    well, we have to convince 2 generations of retired developers. – Sandburg Dec 17 '18 at 11:05