Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C99 - why are false and true defined as 0 and 1 and not as ((bool)0) and ((bool)1)?

Just stumbled across an assert, that failed, as it compared false to the returntype of a function, as the function itself returned a bool and the assert checked not only the value, but also the type of the returnvalue to match the one of false, to guarantee, that a bool is returned. Now the problem is, that C99 defines bool as _Bool and _Bool is even not necessarily same size as int (in fact, in my experience, on most platforms in nowadays it is often same size as unsigned char), not to talk about being same type (which is actually impossible, as _Bool is a builtin type of the language in C99), but defines false and true as 0 and 1 without any typecast and preprocessor definitions without a typecast will default to int. If C99 would instead define false and true as ((bool)0) and ((bool)1), they would always be of type bool, no matter, how _Bool is defined. So is there any good reason to have them always defined as ints, even when bool is not an int on that platform or is this just a bug in the language, that should be fixed with C1x?

like image 679
Kaiserludi Avatar asked Jul 08 '11 16:07

Kaiserludi


People also ask

Is 0 true or false in bool?

Also, a numeric value of zero (integer or fractional), the null value ( None ), the empty string, and empty containers (lists, sets, etc.) are considered Boolean false; all other values are considered Boolean true by default.

Why is 1 true and 0 false?

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. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

Is bool always 0 or 1?

Boolean values and operations 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.

What is #define true 1?

FALSE is 0 and TRUE is all numbers expect 0. If you use #define TRUE 1 : FALSE is 0 and TRUE is 1.


1 Answers

false and true are defined as the integer constants 0 and 1 respectively, because that's exactly what the C99 standard specifies in section 7.16 :

< SNIP >

The remaining three macros are suitable for use in #if preprocessing directives. They are

true

which expands to the integer constant 1,

false

which expands to the integer constant 0, and

< SNIP >

EDIT : as the comments below indicate, it seems I slightly misinterpreted the question, and I should have provided the reason the standard specifies it like that. One reason I can think of, is that true and false are supposed to be usable in #if preprocessing directives (as the quote from the standard mentions).

The reason ((bool) 0) or ((bool) 1) won't work in #if preprocessing directives, is because the standard doesn't allow it. In section 6.10.1 it says :

The expression that controls conditional inclusion shall be an integer constant expression except that: it shall not contain a cast;

like image 138
Sander De Dycker Avatar answered Sep 21 '22 09:09

Sander De Dycker