Velocity Reviews > Which numbers evaluate to true and false?

# Which numbers evaluate to true and false?

Paminu
Guest
Posts: n/a

 10-04-2005
As I remember if(1) evaluates to true and all other numbers including 0
evaluate to false.

the C for dummies books and various other C programming books but nowhere
there is a mention on what a number in an if statement evaluates to.

Is this some kind of big secret?

Christoph Schmidt
Guest
Posts: n/a

 10-04-2005
Nearly. 0 evaluates to false and any other number to true (I am not completely

Cheers,
Chris

Paminu wrote:

> As I remember if(1) evaluates to true and all other numbers including 0
> evaluate to false.
>
> But where do I find out about this for sure?? I have looked through K&R, all
> the C for dummies books and various other C programming books but nowhere
> there is a mention on what a number in an if statement evaluates to.
>
> Is this some kind of big secret?

Skarmander
Guest
Posts: n/a

 10-04-2005
Paminu wrote:
> As I remember if(1) evaluates to true and all other numbers including 0
> evaluate to false.
>

No. if(1) is nothing. If you meant something like

if (1) {
...
}

That doesn't evaluate to anything; it's a statement.

The statements in the "if" branch will be executed if the test
expression evaluates to a non-zero value. Otherwise, the "else" branch
will be executed, if there is one.
if (x) {
...
}
is the same as
if (x != 0) {
...
}

So your statement actually gets it backwards: 0 is "false" (loosely
speaking, since C doesn't have a proper boolean type) and any non-zero
value is "true". But "false" and "true" don't exist as actual values. An
operator like != will yield either 0 or 1, so we could call those
"false" and "true", as long as we keep in mind that C is actually a bit
more liberal than that.

> But where do I find out about this for sure?? I have looked through K&R, all
> the C for dummies books and various other C programming books but nowhere
> there is a mention on what a number in an if statement evaluates to.
>

Probably because you're not reading them quite right. All books I've
read do address this issue, but not exactly in the way you're
interpreting it.

> Is this some kind of big secret?

Yes: C has no boolean type. But I wouldn't call it "big".

S.

Christopher Benson-Manica
Guest
Posts: n/a

 10-04-2005
Paminu <(E-Mail Removed)> wrote:

> As I remember if(1) evaluates to true and all other numbers including 0
> evaluate to false.

No. Expressions that compare equal to 0, including NULL pointers, are
"false". All others are "true".

> But where do I find out about this for sure?? I have looked through K&R, all
> the C for dummies books and various other C programming books but nowhere
> there is a mention on what a number in an if statement evaluates to.

It is on page 223 of my copy of K&R 2.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Keith Thompson
Guest
Posts: n/a

 10-04-2005
Paminu <(E-Mail Removed)> writes:
> As I remember if(1) evaluates to true and all other numbers including 0
> evaluate to false.
>
> But where do I find out about this for sure?? I have looked through K&R, all
> the C for dummies books and various other C programming books but nowhere
> there is a mention on what a number in an if statement evaluates to.
>
> Is this some kind of big secret?

Not at all. See, for example, section 9 of the C FAQ.

BTW, here's what the standard says (my copy of K&R isn't handy):

In both forms, the first substatement is executed if the
expression compares unequal to 0. In the else form, the second
substatement is executed if the expression compares equal to 0. If
the first substatement is reached via a label, the second
substatement is not executed.

The phrase "both forms" refers to "if ( expression ) statement"
vs. "if (expression ) statement else statement".

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson
Guest
Posts: n/a

 10-04-2005
Skarmander <(E-Mail Removed)> writes:
[...]
> Yes: C has no boolean type.

C99 has _Bool (called "bool" with "#include <stdbool.h>").

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Christian Bau
Guest
Posts: n/a

 10-04-2005
In article <dhuo1f\$a2v\$(E-Mail Removed)-c.dk>, Paminu <(E-Mail Removed)>
wrote:

> As I remember if(1) evaluates to true and all other numbers including 0
> evaluate to false.
>
> But where do I find out about this for sure?? I have looked through K&R, all
> the C for dummies books and various other C programming books but nowhere
> there is a mention on what a number in an if statement evaluates to.

from many places, a search for "C Standard Final Draft" in google for
example came up with

http://www.ucalgary.ca/~bgwong/n869.pdf

This is NOT the C Standard, but it is reasonably close for many

http://www.ansi.org/

as far as I know for US\$18.

if (x)

gives exactly the same result as

if ((x) != 0)

x can be any number or any pointer. Therefore,

if (2) printf ("true"); else printf ("false");

will print "true" and not "false".

Skarmander
Guest
Posts: n/a

 10-04-2005
Keith Thompson wrote:
> Skarmander <(E-Mail Removed)> writes:
> [...]
>
>>Yes: C has no boolean type.

>
>
> C99 has _Bool (called "bool" with "#include <stdbool.h>").
>

Pfff. I sort-of semi-deliberately overlooked that, but yes, you're quite
right.

S.

Skarmander
Guest
Posts: n/a

 10-04-2005
Skarmander wrote:
> Keith Thompson wrote:
>
>> Skarmander <(E-Mail Removed)> writes:
>> [...]
>>
>>> Yes: C has no boolean type.

>>
>>
>>
>> C99 has _Bool (called "bool" with "#include <stdbool.h>").
>>

> Pfff. I sort-of semi-deliberately overlooked that, but yes, you're quite
> right.
>

You know, for some reason the C99 bool really annoys me. *Now* they fix
things? Too late. Leave it alone. Introducing size_t to provide a new
level of abstraction was a good thing. But bool? Not worth the bother,
with the backwards compatibility tricks they have to pull.

It ticks me off because I'd *like* to use "real" booleans, but if that
means requiring a C99 compiler, then no thanks, it's not worth it.
Classic chicken and egg story.

S.

Keith Thompson
Guest
Posts: n/a

 10-04-2005
Christian Bau <(E-Mail Removed)> writes:
> In article <dhuo1f\$a2v\$(E-Mail Removed)-c.dk>, Paminu <(E-Mail Removed)>
> wrote:
>
>> As I remember if(1) evaluates to true and all other numbers including 0
>> evaluate to false.
>>
>> But where do I find out about this for sure?? I have looked through K&R, all
>> the C for dummies books and various other C programming books but nowhere
>> there is a mention on what a number in an if statement evaluates to.

>
> from many places, a search for "C Standard Final Draft" in google for
> example came up with
>
> http://www.ucalgary.ca/~bgwong/n869.pdf
>
> This is NOT the C Standard, but it is reasonably close for many
>
> http://www.ansi.org/
>
> as far as I know for US\$18.

You can also get n1124 from
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>. This is
a draft of the *next* version of the standard (post-C99); it's
basically the C99 standard with a few corrections.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.