Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is FALSE == 0?

0 views
Skip to first unread message

Tatu Portin

unread,
Nov 20, 2004, 8:03:59 AM11/20/04
to
Is FALSE == 0?
In other words, does 'function ()' execute on all platforms:

if ( (1 == 2) == 0 )
function ();

if ( (7!) == 0 )
function ();

I would like if you could point to some definition of the c language.

Trent Buck

unread,
Nov 20, 2004, 8:30:47 AM11/20/04
to
Quoth Tatu Portin on or about 2004-11-20:

> Is FALSE == 0?
> In other words, does 'function ()' execute on all platforms:

I asked a similar question recently. Here's the google archive
http://www.google.com/groups?selm=cn0k35%24u6u%242%40rumours.uwaterloo.ca

-trent

Tatu Portin

unread,
Nov 20, 2004, 9:07:40 AM11/20/04
to

From
http://www.google.com/groups?selm=cn0k35%24u6u%242%40rumours.uwaterloo.ca
>All operators that return a boolean result are defined to return 0 or
>1, so you can do this, or at least could if you didn't mind inverting
>the result.

"Are defined to return 0 or 1", but does not say whetever 0 is false or not.
Also, I would like if one could point some place in standard (C89, C99) where
it defines these values.

Goran Larsson

unread,
Nov 20, 2004, 9:16:41 AM11/20/04
to
In article <3hHnd.126$q93...@read3.inet.fi>,
Tatu Portin <axe...@mbnet.fi> wrote:

> Is FALSE == 0?

Yes.

> if ( (1 == 2) == 0 )
> function ();

Yes.

> if ( (7!) == 0 )
> function ();

No, this will not compile.

| if ( (!7) == 0 )
| function ();

Yes.

> I would like if you could point to some definition of the c language.

The ANSI/ISO/IEC 9899-1999 standard.

--
Göran Larsson http://www.mitt-eget.com/

Tatu Portin

unread,
Nov 20, 2004, 9:48:40 AM11/20/04
to
Goran Larsson wrote:
> In article <3hHnd.126$q93...@read3.inet.fi>,
> Tatu Portin <axe...@mbnet.fi> wrote:
>
>>Is FALSE == 0?
>
> Yes.
>
>>if ( (1 == 2) == 0 )
>> function ();
>
> Yes.
>
>>if ( (7!) == 0 )
>> function ();
>
> No, this will not compile.
>
> | if ( (!7) == 0 )
> | function ();
>
> Yes.
>
>>I would like if you could point to some definition of the c language.
>
> The ANSI/ISO/IEC 9899-1999 standard.
>

Thanks. Just if you could give some www-adress for reference.

Michael Mair

unread,
Nov 20, 2004, 10:24:37 AM11/20/04
to
Tatu Portin wrote:

> Goran Larsson wrote:


>
>> Tatu Portin <axe...@mbnet.fi> wrote:
>>> I would like if you could point to some definition of the c
>>> language.
>>
>> The ANSI/ISO/IEC 9899-1999 standard.
>
> Thanks. Just if you could give some www-adress for reference.

The standard is available for about 270 USD as paper copy or from
webstore.ansi.org for 18 USD as .pdf file.
(
http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+9899%2D1999
)

For most beginners, N869, the last public (and freely available) draft
is quite enough. Use google to obtain it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Minti

unread,
Nov 20, 2004, 3:22:34 PM11/20/04
to


I believe your c-book whichever it is should be able to answer this
question.


If it does not maybe you need another book.

--
Imanpreet Singh Arora

Keith Thompson

unread,
Nov 20, 2004, 5:26:16 PM11/20/04
to
Michael Mair <Michae...@invalid.invalid> writes:
[...]

> The standard is available for about 270 USD as paper copy or from
> webstore.ansi.org for 18 USD as .pdf file.
> (
> http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+9899%2D1999
> )
>
> For most beginners, N869, the last public (and freely available) draft
> is quite enough. Use google to obtain it.

For most beginners, a good book is going to be much more useful than a
copy of the standard. K&R2 is one of the best (_The C Programming
Language_, 2nd Edition, by Kernighan & Ritchie).

--
Keith Thompson (The_Other_Keith) ks...@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

unread,
Nov 20, 2004, 5:27:39 PM11/20/04
to

Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.
(If you forget the URL, just google "C FAQ".)

Michael Mair

unread,
Nov 20, 2004, 6:18:41 PM11/20/04
to
Hi Keith,

Keith Thompson wrote:

> Michael Mair <Michae...@invalid.invalid> writes:
> [...]
>
>>The standard is available for about 270 USD as paper copy or from
>>webstore.ansi.org for 18 USD as .pdf file.
>>(
>>http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+9899%2D1999
>>)
>>
>>For most beginners, N869, the last public (and freely available) draft
>>is quite enough. Use google to obtain it.
>
> For most beginners, a good book is going to be much more useful than a
> copy of the standard. K&R2 is one of the best (_The C Programming
> Language_, 2nd Edition, by Kernighan & Ritchie).

Mind the "quite" :-)
The OP asked for the standard, I gave the answer.
He can have a look at N869 and see and believe that going back to
"ordinary" C books might be a better idea. Sometimes, you have to
do it for yourself...
Apart from K&R2, I also always refer the people to the c.l.c FAQ.

Old Wolf

unread,
Nov 21, 2004, 12:10:57 AM11/21/04
to
Tatu Portin <axe...@mbnet.fi> wrote:
> Trent Buck wrote:
> > Quoth Tatu Portin on or about 2004-11-20:
> >
> >>Is FALSE == 0?
> >>In other words, does 'function ()' execute on all platforms:
> >All operators that return a boolean result are defined to return 0 or
> >1, so you can do this, or at least could if you didn't mind inverting
> >the result.
>
> "Are defined to return 0 or 1", but does not say whetever 0 is false or not.

FALSE and false are not part of C. So please rephrase
your question.

> Also, I would like if one could point some place in standard
>(C89, C99) where it defines these values.

FALSE is not defined anywhere. The section on operators says what
the result of each operator is.

Mark McIntyre

unread,
Nov 21, 2004, 6:17:19 AM11/21/04
to
On 20 Nov 2004 21:10:57 -0800, in comp.lang.c , old...@inspire.net.nz (Old
Wolf) wrote:

>FALSE and false are not part of C. So please rephrase
>your question.

Incorrect.

false is a macro defined as zero in stdbool.h

Note however this final sentence:
"Notwithstanding the provisions of 7.1.3, a program may undefine and
perhaps then redefine the macros bool, true, and false"


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Tatu Portin

unread,
Nov 21, 2004, 8:34:33 AM11/21/04
to
Mark McIntyre wrote:
> On 20 Nov 2004 21:10:57 -0800, in comp.lang.c , old...@inspire.net.nz (Old
> Wolf) wrote:
>
>
>>FALSE and false are not part of C. So please rephrase
>>your question.
>
>
> Incorrect.
>
> false is a macro defined as zero in stdbool.h
>
> Note however this final sentence:
> "Notwithstanding the provisions of 7.1.3, a program may undefine and
> perhaps then redefine the macros bool, true, and false"
>
>

Well, I meant that false in general. Like: Is ((1 == 2) == 0)?

Mark McIntyre

unread,
Nov 21, 2004, 10:37:37 AM11/21/04
to
On Sun, 21 Nov 2004 13:34:33 GMT, in comp.lang.c , Tatu Portin
<axe...@mbnet.fi> wrote:

>
>Well, I meant that false in general. Like: Is ((1 == 2) == 0)?

That remark makes no sense.... you need some context for it.

Anyhoo...

The effect of the equality operator is an int with value either one or zero
as appropriate (6.5.9(4)). The macros true and false are defined suitably
(7.16).

What more do you need to know?

Keith Thompson

unread,
Nov 21, 2004, 11:42:47 AM11/21/04
to
Tatu Portin <axe...@mbnet.fi> writes:
> Mark McIntyre wrote:
>> On 20 Nov 2004 21:10:57 -0800, in comp.lang.c , old...@inspire.net.nz (Old
>> Wolf) wrote:
>>
>>>FALSE and false are not part of C. So please rephrase
>>>your question.
>> Incorrect. false is a macro defined as zero in stdbool.h Note
>> however this final sentence:
>> "Notwithstanding the provisions of 7.1.3, a program may undefine and
>> perhaps then redefine the macros bool, true, and false"

This is a new feature in C99.

> Well, I meant that false in general. Like: Is ((1 == 2) == 0)?

The relational operators yield either 0 (for false) or 1 (for true),
as do the "&&", "||", and "!" operators.

A function that returns a value to be used as a condition (e.g., in an
if statement) must return 0 for false, but may return any non-zero
value for true. This includes functions like isdigit() declared in
the standard header <ctype.h>.

The language *could* have been defined so that the relational
operators may yield any non-zero result when the condition is true,
but they were restricted to 0 and 1 for consistency. As a matter of
style, I don't recommend depending on this.

Whenever you test a value that represents a logical (boolean) result,
assume that any non-zero value can be used to represent truth. For
example, suppose you have a function

int argument_is_valid(some_type argument);

and suppose you've defined

#define FALSE 0
#define TRUE 1

This is ok:

if (argument_is_valid(foo)) { ... }

This is needlessly verbose:

if (argument_is_valid(bar) == FALSE) { ... }

And this is dangerous:

if (argument_is_valid(baz) == TRUE ) { ... }

because the function could return 2 to indicate that the argument is
valid.

But using "return TRUE;" or "return FALSE;" in the body of the
function is just fine.

Never compare a boolean value against TRUE or FALSE.

(C99 changes this a bit by introducing a predefined type _Bool or
bool, and predefined values true and false.)

Chris Torek

unread,
Nov 21, 2004, 3:51:16 PM11/21/04
to
In article <news:JP0od.188$yl4...@read3.inet.fi>

Tatu Portin <axe...@mbnet.fi> wrote:
>Well, I meant that false in general. Like: Is ((1 == 2) == 0)?

If I may use a rather ... earthy analogy (which should at least be
unforgettable! :-) ), you can consume many kinds of food and drink:
plain water, orange juice, potatos, chicken, perhaps even lutefisk.
But your digestion of these inputs will only ever produce two kinds
of output, which some call "number one" and "number two"; or as a
friend of mine once called them, "stream output" and "core dumps".

C's relational and logical operators can consume many kinds of
input, but produce only two outputs. For something like:

if (x) {
truestuff();
} else {
falsestuff();
}

C will take the truestuff() branch if x is not zero, and the
falsestuff() branch if x is zero. But for:

int result = (x == y);

result will be 1 -- just just "any nonzero value", but exactly 1
-- if x == y, and 0 (the only "false" value) if x != y.

Note that the functions in <ctype.h>, such as isdigit() and isalpha(),
are *not* required to produce just 0 or 1, and often do not.

Because the logical operators force "any nonzero value" to become
1, and because the "!" (logical-not) operator is its own inverse,
you can use a double negation to "normalize" a boolean result:

int normalized = !!f();

Now "normalized" is 1 if f() returned nonzero, and 0 if f()
returned 0. If for some strange reason you want a "normalized"
version of, e.g., isalpha(), you can write:

int one_if_isalpha = !!isalpha(ch);

The same works for the other logical operators (&& and ||), so you
can also normalize by &&-ing with 1, or ||-ing with 0:

int normalized_via_AND = f() && 1;
int normalized_via_OR = f() || 0;

but I think these are even odder-looking than the "!!" normalization
method.

I leave it up to the reader to decide which of 0 and 1 correspond
to the, er, "stream output" and "core dump". :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Dan Pop

unread,
Nov 23, 2004, 11:57:34 AM11/23/04
to
In <lnsm74k...@nuthaus.mib.org> Keith Thompson <ks...@mib.org> writes:

>Michael Mair <Michae...@invalid.invalid> writes:
>[...]
>> The standard is available for about 270 USD as paper copy or from
>> webstore.ansi.org for 18 USD as .pdf file.
>> (
>> http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+9899%2D1999
>> )
>>
>> For most beginners, N869, the last public (and freely available) draft
>> is quite enough. Use google to obtain it.
>
>For most beginners, a good book is going to be much more useful than a
>copy of the standard. K&R2 is one of the best (_The C Programming
>Language_, 2nd Edition, by Kernighan & Ritchie).

And for ALL posters, reading the FAQ before posting is a *must*!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de
Currently looking for a job in the European Union

Antti-Juhani Kaijanaho

unread,
Nov 27, 2004, 12:59:19 PM11/27/04
to
Michael Mair <Michae...@invalid.invalid> kirjoitti 20.11.2004:
> The standard is available for about 270 USD as paper copy or from
> webstore.ansi.org for 18 USD as .pdf file.

It is also available hardbound from Wiley for the price of a computer
book (Amazon.com's current price appears to be 57,20 USD). I find that
its lack of an un-nice license more than offsets the greater price
compared to the PDF.
--
Antti-Juhani Kaijanaho http://antti-juhani.kaijanaho.info/

Blogi - http://kaijanaho.info/antti-juhani/blog/
Toys - http://www.cc.jyu.fi/yhd/toys/

0 new messages