Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return a==b;

Reply
Thread Tools

return a==b;

 
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On May 16, 12:30 am, Richard Heathfield <(E-Mail Removed)> wrote:
>
>> 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

You'll find C99 + TC 1, 2 and 3 here:
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf

Bye, Jojo


 
Reply With Quote
 
 
 
 
thomas.mertes@gmx.at
Guest
Posts: n/a
 
      05-16-2008
On 16 Mai, 00:12, (E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      05-16-2008
In article <(E-Mail Removed)>,
Richard Heathfield <(E-Mail Removed)> wrote:
....
> 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."

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.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-17-2008
Antoninus Twink wrote, On 15/05/08 23:37:
> On 15 May 2008 at 22:30, Richard Heathfield wrote:
>> Pietro Cerutti said:
>>>> 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 if a and be differ, some implementation-defined value other than 0 if
>>> they're equal (usually 1).

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


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?
--
Flash Gordon
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-19-2008
(E-Mail Removed) wrote:

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

--
/Questions? Answers! Answers? Questions!/ - Focus

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      05-19-2008
In article <(E-Mail Removed)> Antoninus Twink <(E-Mail Removed)> writes:
....
> 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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-19-2008
Dik T. Winter wrote:

> In article <(E-Mail Removed)> Antoninus Twink <(E-Mail Removed)> writes:
> ...
> > 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.


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

--
"If I were you, I would go to the crackpots." /They Shall Have Stars/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments