What is the value of relational expression

S

Sunner Sun

Hi!

In some compiler, the relational expression's value is 1 for true or 0 for
false. Is this a stardard? Or is there any other compilers which use 0 and
other Non-zero value?

Thank you!
Sunner Sun
 
J

Joona I Palaste

Sunner Sun said:
In some compiler, the relational expression's value is 1 for true or 0 for
false. Is this a stardard? Or is there any other compilers which use 0 and
other Non-zero value?

All comparison operators ==, !=, <, >, <= and >= are guaranteed to
return 1 for true and 0 for false. However, any non-zero value can be
used for true in conditional statements.
For example:
int a = 2==2; /* a is guaranteed to be 1 */
if (2) {
printf("This is guaranteed to be executed\n");
}
 
B

Ben Pfaff

Sunner Sun said:
In some compiler, the relational expression's value is 1 for true or 0 for
false. Is this a stardard?
Yes.

Or is there any other compilers which use 0 and other Non-zero
value?

Not C compilers.
 
J

Jack Klein

Hi!

In some compiler, the relational expression's value is 1 for true or 0 for
false. Is this a stardard? Or is there any other compilers which use 0 and
other Non-zero value?

Thank you!
Sunner Sun

The C standard specifies that all relational operators produce a value
of 1 for a true condition, and 0 for a false one. That is absolutely
standard, and no C compiler in existence is likely to get this wrong.

That said, the "as-if" rule allows a compiler to skip producing that
value unless it is actually used as a value, as long as it correctly
recognizes the truth or falsehood of the expression.
 
D

Dan Pop

In said:
In some compiler, the relational expression's value is 1 for true or 0 for
false. Is this a stardard? Or is there any other compilers which use 0 and
other Non-zero value?

How about reading the FAQ *before* posting?

Dan
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top