65

Possible Duplicate:
Error handling in C code
What return value should you use for a failed function call in C?

I always use 0, but its not really readable in if, while, etc.

Should I return 1? Why main function return 0 for success?

Xantium
  • 11,201
  • 10
  • 62
  • 89
ziq
  • 998
  • 1
  • 6
  • 11
  • 2
    The linked question was closed. – mechanical_meat Mar 03 '12 at 19:58
  • @AdamBernier: As a duplicate. It had some highly relevant answers. – Oliver Charlesworth Mar 03 '12 at 19:59
  • @Oli: it doesn't seem appropriate that the other question was closed as a duplicate (maybe I'm just not seeing it). Nevertheless I agree with closing that one and this one: The questions do seem too open-ended for this particular website. – mechanical_meat Mar 03 '12 at 20:02
  • This answer depends on whether you want to return error codes, as opposed to true or false. Usually when having multiple return error codes, 0 is success and other values represent errors. I suppose people use 0 as success and nonzero for failure to remain consistent. – Marlon Mar 03 '12 at 20:16

2 Answers2

60

It's defined by the C standard as 0 for success (credits go to hvd).

But

For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.

(I'm talking about the value returned to the OS from main, exit or similar calls)

As for your function, return what you wish and makes code more readable, as long as you keep it that way along your programs.

Xantium
  • 11,201
  • 10
  • 62
  • 89
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 0 always means success, but other exit codes are also permitted to mean success depending on the system. –  Mar 03 '12 at 19:59
  • @hvd That's not true. The values of `EXIT_SUCCESS` & `EXIT_FAILURE` are not defined in the C standard. – Andrew Marshall Mar 03 '12 at 20:01
  • 2
    @AndrewMarshall `EXIT_SUCCESS` might have some other value than 0, but even then, `return 0;` *also* means success. –  Mar 03 '12 at 20:02
  • @hvd Don't you mean *only* then? If `EXIT_FAILURE` is `0`, then `return 0;` denotes failure. – Andrew Marshall Mar 03 '12 at 20:03
  • 2
    @AndrewMarshall `EXIT_FAILURE` is not allowed to be 0, because returning 0 from main *always* means success. –  Mar 03 '12 at 20:06
  • 2
    @hvd: Indeed, this is implied in C99, 7.20.4.3.5. – Oliver Charlesworth Mar 03 '12 at 20:08
  • 6
    @AndrewMarshall From the C standard: "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." (This is part of the description of the `exit` function, using the same codes.) –  Mar 03 '12 at 20:08
  • @hvd Nice, did not know that. – Andrew Marshall Mar 03 '12 at 20:14
22

The reason why main use 0 for success is that it is used as the exit code of the application to the operating system, where 0 typically means success and 1 (or higher) means failure. (Of course, you should always use the predefined macros EXIT_SUCCESS and EXIT_FAILURE.)

Inside an application, however, it's more natural to use zero for failure and non-zero for success, as the return value can directly be used in an if as in:

if (my_func())
{
  ...
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 11
    "Inside an application, however, it's more natural to use zero for failure and non-zero for success, as the return value can directly be used in an if as in:" I would strongly argue against this, just putting a "!" not only solves the issue, but, it's much more clear and standard to ALWAYS return 0 for success. If the operative system follows it, c standard functions follows it, most of code in the internet follows it, It will confuse the reader. – Santropedro Jul 09 '19 at 11:21