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??

Recommended Answers

All 7 Replies

In c ( or C++) : 0 is false, and any other number is true.

commented: Yes. +6

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.

Wow, people clearly didn't like my simple most obvious answer :P

Wow, people clearly didn't like my simple most obvious answer :P

They didn't like that one either.

>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.

commented: i stand corrected +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.