473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

return a==b;

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
Jun 27 '08 #1
21 1749
go*******@olive-it.ch 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?
yes
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).
thanx
olivier

--
Pietro Cerutti
Jun 27 '08 #2
go*******@olive-it.ch said:
I don't remember the exact syntax, however I've heard that
return a==b;
is valid c
It is.
(like in gcc does not complain)
my questions:
1) is this real C standard?
Yes.
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?
== yields either 0 (a and b have different values) or 1 (a and b have the
same value), guaranteed.

An equality expression is still an expression, so there is no problem using
it in a return statement.

--
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
Jun 27 '08 #3
Pietro Cerutti said:
go*******@olive-it.ch 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?

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

--
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
Jun 27 '08 #4
go*******@olive-it.ch writes:
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".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #5
Richard Heathfield wrote:
Pietro Cerutti said:
>go*******@olive-it.ch 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?
yes
>>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. 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 right (and I'm wrong). I was confusing the result of the
equivalence operator == with conditional expressions, where 'true' is
not guaranteed to be 1.

Thanks.

--
Pietro Cerutti
Jun 27 '08 #6


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


Jun 27 '08 #7
[Adding, not correcting...]

Richard Heathfield wrote:
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?

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

--
Peter
Jun 27 '08 #8
On May 16, 12:30 am, Richard Heathfield <r...@see.sig.invalidwrote:
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
Jun 27 '08 #9
Peter Nilsson <ai***@acay.com.auwrites:
[Adding, not correcting...]

Richard Heathfield wrote:
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?

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #10
go*******@olive-it.ch 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?
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.

Jun 27 '08 #11
Keith Thompson wrote:
Peter Nilsson <ai***@acay.com.auwrites:
Richard Heathfield wrote:
An equality expression is still an expression,
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).
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. :-)

--
Peter
Jun 27 '08 #12
On May 16, 12:31*am, Martin Ambuhl <mamb...@earthlink.netwrote:
google...@olive-it.ch wrote:
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.
I think that paragraph should be taken out and shot :-)

--
Bartc
Jun 27 '08 #13
go*******@olive-it.ch wrote:
On May 16, 12:30 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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
Jun 27 '08 #14
On 16 Mai, 00:12, google...@olive-it.ch 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.
Jun 27 '08 #15
In article <Y7*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
....
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.

Jun 27 '08 #16
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.

Jun 27 '08 #17
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
Jun 27 '08 #18
go*******@olive-it.ch 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

Jun 27 '08 #19
In article <sl*******************@nospam.invalidAntoninus Twink <no****@nospam.invalidwrites:
....
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/
Jun 27 '08 #20
Dik T. Winter wrote:
In article <sl*******************@nospam.invalidAntoninus Twink <no****@nospam.invalidwrites:
...
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

Jun 27 '08 #21
Dik T. Winter said:
In article <sl*******************@nospam.invalidAntoninus Twink
<no****@nospam.invalidwrites: ...
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.
Fortunately, that is never an issue, because all computers in existence
(including mainframes, palmtops, and engine management systems) have an
architecture that is identical in all important respects to the one
sitting on Mr Twink's desk.

--
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
Jun 27 '08 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.