Hello im having trouble understanding between the difference 0 and 1 in C. the way i understood it is that 0 is true and 1 is false. Is this correct??? or is it 0 is false and 1 is true??

there is nothing in C demands that TRUE = 1 and FALSE = 0. macros and constants are only what you #define them to be.

depending on what variation of the C environment you use, you very well may have TRUE defined as 1 and FALSE defined as 0 for you. you may have one or more libraries with an .h file that has the following:

#ifndef FALSE
#define FALSE               0
#endif

#ifndef TRUE
#define TRUE                1
#endif

you may even put that in one of your own .h files.

Sinkula's link, above, warns you to be careful about relying on conditional evaluating something to TRUE (1) when you shouldn't. at best, it can be redundant and unnecessary, at worst it can cause a very hard to find bug.

furthermore the typical concept of FALSE = 0 and TRUE = 1 (or non-zero) is often invalid for many situations, or at least does not follow the assumed perspective. for instance, i write device drivers following a popular convention that defines the return value of the function as:

zero     = success
negative = failure/error code
positive = warning/event info

other functions such as I/O likewise will use negative values as failure/error codes, and may have positive values be the successful return of valid information. these positive values will often likely not be 1, but be some larger number.

so back up and revisit your assumptions on what TRUE and FALSE mean to you. you might be putting too much value on it.

Edited 6 Years Ago by jephthah: n/a

>there is nothing in C demands that TRUE = 1 and FALSE = 0.
Depends on the context. If you mean at the language level, then yes, relational comparisons are required to treat zero as false and non-zero as true.

>for instance, i write device drivers following a popular
>convention that defines the return value of the function as:
>zero = success
>negative = failure/error code
>positive = warning/event info

That's not a boolean context, and thus true/false is nonsensical. You don't have to describe device drivers to give an example of that kind of behavior. The main function does it, where 0 is a success code and non-zero denotes a failure code.

>other functions such as I/O likewise will use negative values as failure/error codes,
>and may have positive values be the successful return of valid information

Once again, not a boolean context. True/false doesn't apply here either.

Comments
i stand corrected
This article has been dead for over six months. Start a new discussion instead.