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.
2 Answers
More accurately anything that is not 0
is true.
So 1
is true, but so is 2
, 3
... etc.
-
4The question seems to refer to specific entities:
bool
true
andfalse
, not to general set of values that "makesif
take the true branch". Oct 12, 2016 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. Oct 12, 2016 at 22:03
-
5Depends on what you mean by "is true". The identifier
true
is a macro defined in<stdbool.h>
, and its value is specifically1
(and its type isint
). On the other hand, any non-zero value is treated as true when used as a condition. That's why something likeif (b == true)
is dangerous; just writeif (b)
instead. Oct 12, 2016 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: thebool
type is completely useless except possibly to distiguish from those languages wheretrue
is-1
, but the same applies:0
is false, anything else is true. Oct 12, 2016 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)
Oct 12, 2016 at 23:16
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.
-
2
-
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 :/ Oct 12, 2016 at 22:10
-
2
-
@KeithThompson: Indeed. Absent any guidance from the original poster I simply picked the first standard that a search turned up. Thanks for the link. Oct 12, 2016 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 theint
1
. I can give chapter and verse for each of those if you are unable to find it.– M.MOct 12, 2016 at 22:20
true == 1
andfalse == 0
are both true. (Andtrue == 2
is not true). If that's not what you meant, could you clarify the question?while(X)
is equivalent towhile( (X) != 0 )