LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why 'return 0'? (https://www.linuxquestions.org/questions/programming-9/why-return-0-a-4175455323/)

stf92 03-23-2013 11:11 PM

Why 'return 0'?
 
Hi: Language C: 0 is false, !=0 is true. In fact true is 0xffff...f, any number of efs, because it is all ones in binary. OK. For a regular function I return with 'return 0'. So, if f is a funcion, a= f is a= false. Why?

stf92 03-23-2013 11:32 PM

It's an exit code!

AnanthaP 03-23-2013 11:59 PM

Usually the high bit is reserved for the sign (signed data types being more usual).

OK

psionl0 03-24-2013 10:23 AM

It makes perfect sense if you regard the exit code as an ENCOUNTERED_AN_ERROR flag. (As a bonus, the return value indicates which error was encountered).

stf92 03-24-2013 12:46 PM

It's clear. As in
Code:

if(some_function())
{
        /* print error message here */
       
        /* return an exit code */
        return 1;
}

some_function is called, but within an if. At the same time, the caller signals an error in the return clause.

psionl0 03-24-2013 07:13 PM

Quote:

Originally Posted by stf92 (Post 4917811)
Code:

if(error_code = some_function())
{
        /* print error message here */
       
        /* return an exit code */
        return error_code;
}


Assuming that some_function() returns sensible error codes, it would make more sense to return that value rather than the generic "1".

Quote:

Originally Posted by stf92 (Post 4917811)
some_function is called, but within an if. At the same time, the caller signals an error in the return clause.

some_function is always called in this code. Only the statements in the curly braces are conditionally executed.

stf92 03-24-2013 07:20 PM

Of course. Thank you for you useful posts. psionl0.

"some_function is always called in this code. Only the statements in the curly braces are conditionally executed". No doubt about it.

mina86 03-25-2013 01:22 PM

Quote:

Originally Posted by stf92 (Post 4917520)
In fact true is 0xffff...f, any number of efs, because it is all ones in binary.

That's incorrect.

Quote:

Originally Posted by stf92 (Post 4917520)
For a regular function I return with 'return 0'. So, if f is a funcion, a= f is a= false. Why?

For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.

psionl0 03-25-2013 11:32 PM

Quote:

Originally Posted by mina86 (Post 4918577)
That's incorrect.

Yes, C defines TRUE to be 1 and not -1. It stems from the early days of C programming when the results of boolean tests were frequently used in arithmetic computations.

eg:
Code:

y = ((x == 5) * some_other_computation(x));
Such clever tactics are poor programming practice though. It is much clearer to use
Code:

if (x == 5)
    y = 0;
else
    y = some_other_computation(x);


Quote:

Originally Posted by mina86 (Post 4918577)
For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.

open() returns the file descriptor on success but to avoid using 0 to indicate failure (when exit uses it to indicate success) it returns -1 instead since file descriptors are always positive numbers. This illustrates a limitation of C in that it can only return a single value or a pointer to a structure (or a structure itself nowadays). The latter option would be unnecessarily complex.

mina86 03-26-2013 09:01 AM

Quote:

Originally Posted by psionl0 (Post 4918910)
Yes, C defines TRUE to be 1 and not -1.

C does not define TRUE at all. C99 defines _True keywoard and to makes things easier true macro when including stdbool.h, but there is no TRUE. It is correct however that boolean operators return 0 or 1.

Quote:

Originally Posted by psionl0 (Post 4918910)
open() returns the file descriptor on success but to avoid using 0 to indicate failure (when exit uses it to indicate success) it returns -1 instead since file descriptors are always positive numbers.

File descriptors are not always positive numbers. Standard input is zero which is not positive.

Quote:

Originally Posted by psionl0 (Post 4918910)
This illustrates a limitation of C in that it can only return a single value or a pointer to a structure (or a structure itself nowadays). The latter option would be unnecessarily complex.

Functions can return structures since at least C89. I'm not sure how you define “nowadays”.

NevemTeve 03-27-2013 07:18 AM

To summarize: always read the documentation of the actual function.
Examples:
malloc, fopen: NULL=error
open: -1=error
read: -1=error,0=eof
isatty: 1=yes,0=no
inet_aton: 0=error

H_TeXMeX_H 03-27-2013 07:31 AM

Quote:

Originally Posted by mina86 (Post 4918577)
For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.

I agree, this is the way it has to be to make sense. The return value has to be interpreted.

chrism01 04-02-2013 02:38 AM

There's also the conceptual approach; there's a variable number of ways a fn can fail (& why not supply a different value for each one), but you only need one value for success ... :)

psionl0 04-02-2013 03:24 AM

Quote:

Originally Posted by chrism01 (Post 4923297)
There's also the conceptual approach; there's a variable number of ways a fn can fail (& why not supply a different value for each one), but you only need one value for success ... :)

That's pretty much what I said in the beginning (0 = no failure). However, there are a number of ways to succeed (eg return the address of memory that was allocated).

In the absence of returning a structure that includes an error code, a single return value has to include some way of indicating error codes as well as success codes.

So, what H_TeXMeX_H said ...

bloody 04-02-2013 08:19 PM

If a C/C++ function states to use boolean return codes, then 0 will be false. I always test against 0 (false) and treat anything else as true, no matter if stdbool.h, C++, GTK or whatever, doesn't matter if true is defined as 1 (the common standard for C/C++) or even -1. Just treat 0 as false and you're good. Still, check if the API you're using is defining an own boolean type and then use that type.

Other functions return a different return value type (other than bool, e.g., int), sometimes using a return code of zero for success and a range of other codes for multiple different outcomes, so they can pass additional information in the returncode.

A software that exits (e.g., back to the shell it was launched from) either returns EXIT_SUCCESS (0) or some kind of error/failure returncode. Not boolean either.


All times are GMT -5. The time now is 07:55 PM.