return a==b;

G

google.bo

I don't remember the exact syntax, however I've heard that
return a==b;
is valid c (like in gcc does not complain)
my questions:
1) is this real C standard?
2) as there is no true nor false in C (just 0 and not 0) what does
a==b have for a value, if any?
thanx
olivier
 
K

Keith Thompson

I don't remember the exact syntax, however I've heard that
return a==b;
is valid c (like in gcc does not complain)

Yes, it's valid C (assuming appropriate declarations of a and b), and
yes, I expect that gcc wouldn't complain about it. (There are a number
of things, gcc-specific extensions, that gcc doesn't complain about
by default, even though they aren't valid C (though just what "valid C"
means is subject to some debate).)
my questions:
1) is this real C standard?
2) as there is no true nor false in C (just 0 and not 0) what does
a==b have for a value, if any?

As your C reference should tell you, the "==" operator yields a result
of type int, with the value 1 if the operands are equal and 0 if they
are not.

In general, built-in operators that yield "boolean" results yield 0
for false, 1 for true. Library functions such as isupper() return 0
for false, some unspecified non-zero value for true. Contexts that
require a condition (such as an if or while) treat 0 as false,
anything else as true.

(C99 adds a built-in boolean type, but all the existing rules stay the
same.)

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 9 covers
"Boolean Expressions and Variables".
 
A

Antoninus Twink

Pietro Cerutti said:

That's not how I read the Standard.

Or just apply common sense. At the machine code level, there will be a
comparison instruction followed by one that sets a register to the
current value (0 or 1) of the zero flag.
 
W

Walter Banks

Richard said:
That's not how I read the Standard. Could you please provide chapter and
verse? (Mine are 3.3.8: "Each of the operators < (less than), > (greater
than), <= (less than or equal to), and >= (greater than or equal to) shall
yield 1 if the specified relation is true and 0 if it is false." and
3.3.9: "The == (equal to) and the != (not equal to) operators are
analogous to the relational operators except for their lower precedence".
This suggests strongly that == must yield either 0 or 1. If it's actually
implementation-defined, the Standard will document this fact somewhere,
but I can find no such reference.)

You are correct. It is 0 or 1 not implementation defined.

if (a) ....

tests 0 or non zero

a = b < c; returns 0 or 1

w..
 
P

Peter Nilsson

[Adding, not correcting...]

Richard said:
== yields either 0 (a and b have different values) or 1 (a and
b have the same value), guaranteed.

With technical exceptions of trap representations and signaling
floating point numbers.
An equality expression is still an expression,

Moreover, relational and equality operators are just operators.
so there is no problem using it in a return statement.

Unless it's used in a void function.
 
G

google.bo

That's not how I read the Standard. Could you please provide chapter and
verse? (Mine are 3.3.8: "Each of the operators < (less than), > (greater
than), <= (less than or equal to), and >= (greater than or equal to) shall
yield 1 if the specified relation is true and 0 if it is false." and
3.3.9: "The == (equal to) and the != (not equal to) operators are
analogous to the relational operators except for their lower precedence".
This suggests strongly that == must yield either 0 or 1. If it's actually
implementation-defined, the Standard will document this fact somewhere,
but I can find no such reference.)
Is there a pointer to "The Standard" you refer? I have just the K&R
book, and yes, I just found there the reference, hidden in chapter
2.6:
"By definition, the numeric value of a relational or logical
expression is 1 if the relation is true, and ' if the relation is
false."
That's my reference:)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

olivier
 
K

Keith Thompson

Peter Nilsson said:
[Adding, not correcting...]

Richard said:
== yields either 0 (a and b have different values) or 1 (a and
b have the same value), guaranteed.

With technical exceptions of trap representations and signaling
floating point numbers.
An equality expression is still an expression,

Moreover, relational and equality operators are just operators.
so there is no problem using it in a return statement.

Unless it's used in a void function.

Or in a function that returns a non-arithmetic type (a pointer,
struct, or union).
 
M

Martin Ambuhl

I don't remember the exact syntax, however I've heard that
return a==b;
is valid c (like in gcc does not complain)
my questions:
1) is this real C standard?

It's trivial standard. 'a==b' is an expression with a value, and you
return that value.
2) as there is no true nor false in C (just 0 and not 0) what does
a==b have for a value, if any?

That's not quite right. There are many operators and functions that
yield a 'true' or 'false' result. Since 'true' and 'false' are
philosophical concepts and not values, the results actually produces for
predicate operations and functions are 1 and 0. Only in interpreting
results as predicates but from from operations and functions which are
not essentially predicates is it true that non-zero is equivalent to 1.
 
P

Peter Nilsson

Keith said:
Or in a function that returns a non-arithmetic type (a pointer,
struct, or union).

In the case of a pointer, it may be used in a null pointer constant
context, e.g. ...

void *null(void) { return 0 == 1; }

....though that's not advisable. :)
 
B

Bart

(e-mail address removed) wrote:

That's not quite right.  There are many operators and functions that
yield a 'true' or 'false' result.  Since 'true' and 'false' are
philosophical concepts and not values, the results actually produces for
predicate operations and functions are 1 and 0.  Only in interpreting
results as predicates but from from operations and functions which are
not essentially predicates is it true that non-zero is equivalent to 1.

I think that paragraph should be taken out and shot :)
 
T

thomas.mertes

I don't remember the exact syntax, however I've heard that
return a==b;
is valid c (like in gcc does not complain)
my questions:
1) is this real C standard?
2) as there is no true nor false in C (just 0 and not 0) what does
a==b have for a value, if any?
thanx
olivier

As explained already in other mails a==b is okay and delivers
0 or 1. So it is exactly defined. It can be used in every
place where an integer (boolean) expression is allowed.

Maybe you confused the behaviour of == (which is exactly
defined) with the behaviour of strcmp and memcmp, which are
not defined to return one of the values -1, 0 and 1.
Instead strcmp and memcmp can return any integer value.

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
K

Kenny McCormack

Richard Heathfield said:
10 108 629

This is typical Heathfield. What Richard refers to as his
(Heathfield's) excessively flowery language - with his usual sarcasm
thrown in.

All that was necessary was to say:

"No, it has to be 1 (see 3.3.8)"

For which, wc reports:
1 8 33

P.S. Oops! I just noticed that I have neglected to "undo" the piping
of Heathfield's response through wc. Oh well, since said piping results in
no reduction in the value or content of his words, I guess there is no
problem there.
 
F

Flash Gordon

Antoninus Twink wrote, On 15/05/08 23:37:
Or just apply common sense. At the machine code level, there will be a
comparison instruction followed by one that sets a register to the
current value (0 or 1) of the zero flag.

Apart from on processors with no such instruction, and I've used several
where you could could not directly set a register on the basis of a
condition flag, only branch on it. Having branched setting a register to
all bits 1 (or writing all bits 1 to memory) would be just as easy as
setting it to a value of 1. Why do you think that several BASICs use 0
and -1?
 
C

Chris Dollin

I don't remember the exact syntax, however I've heard that
return a==b;
is valid c (like in gcc does not complain)

The "(like in gcc does not complain)" isn't an exact match to
"is valid c". Be warned.
my questions:
1) is this real C standard?

Yes. `return` can have an expression operand. `a == b` is a legal
expression, assuming `a` and `b` have been declared and have
appropriate types (eg not structs). The result type is `int`,
so the function this appears in had better have a compatible
return-type.
2) as there is no true nor false in C (just 0 and not 0) what does
a==b have for a value, if any?

0 or 1; all the relational operators return 0 or 1.
 
D

Dik T. Winter

> Or just apply common sense. At the machine code level, there will be a
> comparison instruction followed by one that sets a register to the
> current value (0 or 1) of the zero flag.

That is not much common sense on a machine that does not have a zero
flag.
 
C

Chris Dollin

Dik said:
That is not much common sense on a machine that does not have a zero
flag.

Or one that doesn't have an instruction to copy the flag into
a register. (I can't think of a 1-instruction ARM sequence that
does it; two-instruction sequences are of course trivial and
branch-free. Maybe newer ARMs can do this, or my aged memory is
dropping bts.)
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top