Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c, in bool, true == 1 and false == 0?

Tags:

c

boolean

Just to clarify I found similar answer but for C++, I'm kinda new to coding so I'm not sure whether it applies to C as well.

like image 320
Piotrek Karpowicz Avatar asked Oct 12 '16 21:10

Piotrek Karpowicz


People also ask

Is bool true 1 or 0?

The boolean type. The bool represents a value, which could only be either true or false . If you cast a bool into an integer, true will be 1 and false will be 0.

Is 0 false or true in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

What is true and false in bool?

There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers. C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.

Why is 1 true and 0 false?

1 is considered to be true because it is non-zero. The fourth expression assigns a value of 0 to i. 0 is considered to be false.


Video Answer


2 Answers

More accurately anything that is not 0 is true.

So 1 is true, but so is 2, 3 ... etc.

like image 172
Neil Locketz Avatar answered Oct 19 '22 16:10

Neil Locketz


You neglected to say which version of C you are concerned about. Let's assume it's this one:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

As you can see by reading the specification, the standard definitions of true and false are 1 and 0, yes.

If your question is about a different version of C, or about non-standard definitions for true and false, then ask a more specific question.

like image 5
Eric Lippert Avatar answered Oct 19 '22 15:10

Eric Lippert