Relational Operators

A

AliceB.Toklas

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
 
B

Ben Pfaff

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?
 
A

AliceB.Toklas

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

the follow is true:

i == 1
 
B

Ben Pfaff

[My book states that, given]
int i = 5

the follow is true:

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

Robert Gamble

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
 
R

Richard Bos

Well my book lists == under relational operators,

Then the "Absolute Beginner's" in the title may well be a statement
about its author.
int i = 5

the follow is true:

i == 1

Of course not.

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

Richard
 
A

August Karlstrom

Robert Gamble skrev:
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
 
A

August Karlstrom

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
 
B

Ben Pfaff

August Karlstrom said:
Ben Pfaff skrev:

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
 
A

August Karlstrom

Ben Pfaff skrev:
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
 
R

Richard Heathfield

August Karlstrom said:
Ben Pfaff skrev:
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.
 
C

CBFalconer

Ben said:
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
 
E

Eric Sosman

Richard said:
August Karlstrom said:



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

August Karlstrom

Richard Heathfield skrev:
August Karlstrom said:



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
 
K

Keith Thompson

August Karlstrom said:
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
=, but not == and !=, as "relational operators", just doesn't bother
me that much.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top