33

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.

serenesat
  • 4,611
  • 10
  • 37
  • 53
Piotrek Karpowicz
  • 363
  • 1
  • 3
  • 4
  • Arguably a duplicate of [Using boolean values in C](http://stackoverflow.com/q/1921539/11683) – GSerg Oct 12 '16 at 21:59
  • 1
    The expressions `true == 1` and `false == 0` are both true. (And `true == 2` is not true). If that's not what you meant, could you clarify the question? – M.M Oct 12 '16 at 22:03
  • @M.M Oh yes, Im sorry now I see what I just wrote. I mean when you want to use something in while or if , do you use (lets say) while(blabla == 0) , or something else. – Piotrek Karpowicz Oct 12 '16 at 22:07
  • 1
    `while(X)` is equivalent to `while( (X) != 0 )` – M.M Oct 12 '16 at 22:17
  • Possible duplicate of [Using true and false in C](https://stackoverflow.com/questions/2254075/using-true-and-false-in-c) – phuclv May 23 '17 at 15:00
  • @M.M Are you sure you aren't only talking about stdbool.h? "everywhere" else 2 == true... – The incredible Jan Sep 21 '22 at 13:09
  • @TheincredibleJan `true` is defined by `stdbool.h` which is part of the C Standard . I am talking about the C language , unsurprisingly – M.M Sep 22 '22 at 01:11

2 Answers2

69

More accurately anything that is not 0 is true.

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

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34
  • 4
    The question seems to refer to specific entities: `bool` `true` and `false`, not to general set of values that "makes `if` take the true branch". – AnT stands with Russia Oct 12 '16 at 22:01
  • 2
    @AnT if he didn't say he was new to programming I would have gone into that. But it is easy to tell that this is what he meant. – Neil Locketz Oct 12 '16 at 22:03
  • 5
    Depends on what you mean by "is true". The identifier `true` is a macro defined in ``, and its value is specifically `1` (and its type is `int`). On the other hand, any non-zero value is treated as true when used as a condition. That's why something like `if (b == true)` is dangerous; just write `if (b)` instead. – Keith Thompson Oct 12 '16 at 22:12
  • And that is why IMO the `bool` is of use only for setting a value, not testing. For testing, it's inherent in the C langauge without any formal definition. I'll go further: the `bool` type is completely useless except possibly to distiguish from those languages where `true` is `-1`, but the same applies: `0` is false, anything else is true. – Weather Vane Oct 12 '16 at 22:17
  • More accurately anything that _compares equal to 0_ is false. A null pointer may not be 0, yet compares equally to 0 and so is false as in `if(null_pointer)` – chux - Reinstate Monica Oct 12 '16 at 23:16
  • This answer helped me to understand : 0 and 'odd' Out[]: 0 :::: 1 and 'odd' Out[]: 'odd' – dgor Mar 10 '22 at 06:51
6

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 2
    Yet another weird quirk of C... `true` is an int, not a bool – M.M Oct 12 '16 at 22:05
  • Thanks, that did help, as someone pointed out I didnt specify the question very well, but you managed to answer it anyway, thanks again! , and Im sorry, I really dont know where to even check which version :/ Im really green at this point :/ – Piotrek Karpowicz Oct 12 '16 at 22:10
  • 2
    [N1256](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) is a draft of the 1999 ISO C standard. The latest standard is C11; the latest draft is [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). `_Bool` and `` were added to the language by C99 (and of course retained in C11). – Keith Thompson Oct 12 '16 at 22:10
  • @KeithThompson: Indeed. Absent any guidance from the original poster I simply picked the first standard that a search turned up. Thanks for the link. – Eric Lippert Oct 12 '16 at 22:12
  • 1
    @EricLippert `bool` is a typedef for a type `_Bool` which is an integer type distinct from any other integer type. `true` is a macro that expands to the `int` `1`. I can give chapter and verse for each of those if you are unable to find it. – M.M Oct 12 '16 at 22:20
  • @M.M: Ah, I see the distinction you are making now; it is the distinction between the *canonical type of an expression*, and *the set of values which are compatible with a type*. I withdraw my objection. Incidentally, C# makes a similar distinction; the literal `0` is assignable to any variable of any enum type, despite having type `int`, which is itself not implicitly convertible to any enum type. – Eric Lippert Oct 12 '16 at 22:25
  • @M.M Does "`true` which expands to the integer constant 1," (C11 7.18) or something else require `true` to be type `int` or could it be type `unsigned` or even some other integer type? – chux - Reinstate Monica Oct 12 '16 at 22:51
  • @chux see N1570 6.4.4.1 for the definition of "integer constant" plus a table for determining the type . In C "integer constant" is what C++ calls "integer literal" (I think the latter is a more intuitive terminology). I think it's clear that "integer constant 1" means `1` although I see where you are looking for a wiggle room. – M.M Oct 12 '16 at 22:55
  • @M.M I just never considered the type of `true`. Spec uses "integer constant 1" (unformatted 1) and "integer constant `1`" (formatted 1) in various places. With `true`, it is the unformatted 1. OTOH, I think somewhere in the spec, formatting is informative and not normative, so that means little. – chux - Reinstate Monica Oct 12 '16 at 23:05