Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Which numbers evaluate to true and false? (http://www.velocityreviews.com/forums/t439670-which-numbers-evaluate-to-true-and-false.html)

Paminu 10-04-2005 08:17 PM

Which numbers evaluate to true and false?
 
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?

Christoph Schmidt 10-04-2005 08:24 PM

Re: Which numbers evaluate to true and false?
 
Nearly. 0 evaluates to false and any other number to true (I am not completely
sure about floating point).

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 10-04-2005 08:26 PM

Re: Which numbers evaluate to true and false?
 
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 10-04-2005 08:46 PM

Re: Which numbers evaluate to true and false?
 
Paminu <jadajada@asd.com> 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 10-04-2005 08:51 PM

Re: Which numbers evaluate to true and false?
 
Paminu <jadajada@asd.com> 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) kst-u@mib.org <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 10-04-2005 08:52 PM

Re: Which numbers evaluate to true and false?
 
Skarmander <invalid@dontmailme.com> writes:
[...]
> Yes: C has no boolean type.


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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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 10-04-2005 09:13 PM

Re: Which numbers evaluate to true and false?
 
In article <dhuo1f$a2v$1@news.net.uni-c.dk>, Paminu <jadajada@asd.com>
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.


You can download a copy of the Final Draft of the C99 Standard for free
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
purposes. You can download the C Standard from

http://www.ansi.org/

as far as I know for US$18.

To answer your original question,

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 10-04-2005 09:33 PM

Re: Which numbers evaluate to true and false?
 
Keith Thompson wrote:
> Skarmander <invalid@dontmailme.com> 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 10-04-2005 09:43 PM

Re: Which numbers evaluate to true and false?
 
Skarmander wrote:
> Keith Thompson wrote:
>
>> Skarmander <invalid@dontmailme.com> 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 10-04-2005 09:44 PM

Re: Which numbers evaluate to true and false?
 
Christian Bau <christian.bau@cbau.freeserve.co.uk> writes:
> In article <dhuo1f$a2v$1@news.net.uni-c.dk>, Paminu <jadajada@asd.com>
> 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.

>
> You can download a copy of the Final Draft of the C99 Standard for free
> 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
> purposes. You can download the C Standard from
>
> 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) kst-u@mib.org <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.


All times are GMT. The time now is 06:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57