ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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).
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
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”.
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
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.
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
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
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”.
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
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.
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 ...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.