469,646 Members | 1,359 Online

# Relational Operators

In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:

int i = 5;

so the following statement is true:

int i == 1

Um, to me that says integer i is 5, and 5 is equal to 1.

Can someone help me understand this?
Thanks

Mar 4 '07 #1
14 2314
Al***********@gmail.com writes:
In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:

int i = 5;

so the following statement is true:

int i == 1
== is not a relational operator (it is an equality operator) and
the latter is not a statement, nor is it a valid declaration.
Are you sure that you've typed that in exactly as it appears in
the book?
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Mar 4 '07 #2
On Mar 4, 4:55 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
AliceB.Tok...@gmail.com writes:
In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:
int i = 5;
so the following statement is true:
int i == 1

== is not a relational operator (it is an equality operator) and
the latter is not a statement, nor is it a valid declaration.
Are you sure that you've typed that in exactly as it appears in
the book?
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup

Well my book lists == under relational operators, and my use of the
word statement was also directly from the book. Though, I'm quite
aware that I haven't got the lingo down. Anyway, there was one
mistake in my post, but I don't think it changes the fact it isn't a
valid declaration
int i = 5

i == 1

Mar 4 '07 #3
Al***********@gmail.com writes:
[My book states that, given]
int i = 5

i == 1
Nope, it's not true. That is, i == 1 is false if i has the value
5. If your book really states that, it's just wrong.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Mar 5 '07 #4
On Mar 4, 5:48 pm, AliceB.Tok...@gmail.com wrote:
In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:

int i = 5;

so the following statement is true:

int i == 1
Here is what your book actually says:

int i = 5;
int j = 10;
int k = 15;
int l = 5;
....
i == l
j < k
....

The author is saying that the value of the variable "i" is equal to
the value of the variable "l" (the letter l, not the number 1), etc.
The author could probably have picked better variable names.

Robert Gamble

Mar 5 '07 #5
Al***********@gmail.com wrote:
On Mar 4, 4:55 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
AliceB.Tok...@gmail.com writes:
In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:
int i = 5;
so the following statement is true:
int i == 1
== is not a relational operator (it is an equality operator) and
the latter is not a statement, nor is it a valid declaration.
Are you sure that you've typed that in exactly as it appears in
the book?

Well my book lists == under relational operators,
Then the "Absolute Beginner's" in the title may well be a statement
int i = 5

i == 1
Of course not.

If the book really makes both of these errors, ditch it. Try K&R

Richard
Mar 5 '07 #6
Robert Gamble skrev:
On Mar 4, 5:48 pm, AliceB.Tok...@gmail.com wrote:
>In my Absolute Beginner's Guide to C book by Greg Perry
where it is instruction how relational operators work it gives the
following example:
[...]
The author is saying that the value of the variable "i" is equal to
the value of the variable "l" (the letter l, not the number 1), etc.
The author could probably have picked better variable names.
Indeed. To give an identifier the name l is very bad practice.
August
Mar 5 '07 #7
Ben Pfaff skrev:
== is not a relational operator (it is an equality operator)
Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.
August
Mar 5 '07 #8
August Karlstrom <fu********@comhem.sewrites:
Ben Pfaff skrev:
>== is not a relational operator (it is an equality operator)

Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.
Not in C.

6.5.8 Relational operators
Syntax
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

6.5.9 Equality operators
Syntax
1 equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression != relational-expression

--
Bite me! said C.
Mar 5 '07 #9
Ben Pfaff skrev:
August Karlstrom <fu********@comhem.sewrites:
>Ben Pfaff skrev:
>>== is not a relational operator (it is an equality operator)
Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.

Not in C.

6.5.8 Relational operators
Syntax
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

6.5.9 Equality operators
Syntax
1 equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression != relational-expression
Laughable
August
Mar 5 '07 #10
August Karlstrom said:
Ben Pfaff skrev:
>August Karlstrom <fu********@comhem.sewrites:
>>Ben Pfaff skrev:
== is not a relational operator (it is an equality operator)
Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.

Not in C.
<proof snipped>
>
Laughable
Not at all. The relational operators test an ordering - a relation -
between two objects. == doesn't do that. It tests only whether two
objects are equal, and yields no relational information whatsoever.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 5 '07 #11
Ben Pfaff wrote:
August Karlstrom <fu********@comhem.sewrites:
>Ben Pfaff skrev:
>>== is not a relational operator (it is an equality operator)

Of course it is -- in the same sense as a car is a vehicle.
Other relational operators are <= and >=.

Not in C.

6.5.8 Relational operators
Syntax
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

6.5.9 Equality operators
Syntax
1 equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression != relational-expression
The reason being that the two kinds need separating, since you can
use equality operators on any two pointers, but not necessarily
relational operators.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 6 '07 #12
Richard Heathfield wrote:
August Karlstrom said:
>Ben Pfaff skrev:
>>August Karlstrom <fu********@comhem.sewrites:

Ben Pfaff skrev:
== is not a relational operator (it is an equality operator)
Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.
Not in C.
<proof snipped>
>Laughable

Not at all. The relational operators test an ordering - a relation -
between two objects. == doesn't do that. It tests only whether two
objects are equal, and yields no relational information whatsoever.
... and on a more practical level, the equality operators
can be used in situations where the relational operators would
produce undefined behavior:

int this;
char whatnot[97];
int that;

if (&this == &that) ... /* valid, false */
if (&this < &that) ... /* undefined behavior */

The two kinds of operators behave differently, so the
difference between them is not in the least "laughable."

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 6 '07 #13
Richard Heathfield skrev:
August Karlstrom said:
>Ben Pfaff skrev:
>>August Karlstrom <fu********@comhem.sewrites:

Ben Pfaff skrev:
== is not a relational operator (it is an equality operator)
Of course it is -- in the same sense as a car is a vehicle. Other
relational operators are <= and >=.
Not in C.
<proof snipped>
>Laughable

Not at all. The relational operators
in C terminology
test an ordering - a relation -
between two objects.
Exactly, these are *order* relations.
== doesn't do that.
It tests only whether two
objects are equal, and yields no relational information whatsoever.
Correct, equality is not an order relation.

Generally, if x and y are elements of the set M, a relation between x
and y is defined as a subset of M^2 (Cartesian product). So equality is
obviously a relation.

What's laughable is using the term relation but without letting equality
be an instance of it. They should have used a different name for the
order relations and the shift expressions.
August
Mar 6 '07 #14
August Karlstrom <fu********@comhem.sewrites:
[...]
Generally, if x and y are elements of the set M, a relation between x
and y is defined as a subset of M^2 (Cartesian product). So equality is
obviously a relation.

What's laughable is using the term relation but without letting equality
be an instance of it. They should have used a different name for the
order relations and the shift expressions.
Yeah, and a "byte" should be exactly 8 bits, and an "object" should be
something with methods and inheritance, and a "string" should be a
data type.

The authors of the standard had to express technical concepts using
English words, introducing explicit definitions where necessary. They
may not have done a perfect job of picking the best term in each
instance, but IMHO, they did pretty well. Referring to <, <=, >, and
>=, but not == and !=, as "relational operators", just doesn't bother
me that much.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 6 '07 #15

### This discussion thread is closed

Replies have been disabled for this discussion.