Is FALSE == 0?

T

Tatu Portin

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.
 
T

Tatu Portin

Trent said:
Quoth Tatu Portin on or about 2004-11-20:



I asked a similar question recently. Here's the google archive
http://www.google.com/[email protected]

-trent
From
http://www.google.com/[email protected]
>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.
 
G

Goran Larsson

Tatu Portin said:
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.
 
T

Tatu Portin

Goran said:
No, this will not compile.

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

Yes.


The ANSI/ISO/IEC 9899-1999 standard.

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

Minti

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


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


If it does not maybe you need another book.
 
K

Keith Thompson

Michael Mair said:
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/ISO/IEC+9899-1999
)

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).
 
M

Michael Mair

Hi Keith,

Keith said:
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.


Cheers
Michael
 
O

Old Wolf

Tatu Portin said:
"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.
 
M

Mark McIntyre

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"
 
T

Tatu Portin

Mark said:
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)?
 
M

Mark McIntyre

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?
 
K

Keith Thompson

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.)
 
C

Chris Torek

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". :)
 
D

Dan Pop

In said:
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
 
A

Antti-Juhani Kaijanaho

Michael Mair said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top