operands for relational operators <, >, >= etc

G

Greenhorn

Hi,

Those relational operators have operands of all numerical type
int,char,float etc.
They also are working for character arrays. Whats the logic behind
their working. Is the length of the array compared first and then each
character compared with corresponding character.

{
const char msg[] = "msessage}", ch [] = "Za";
if(msg > ch)
printf("This is working, ch %s is less than msg", ch);
}

whats the morale behind including arrays as valid operators for these
operators?

greenhorn.
 
E

Eric Sosman

Greenhorn said:
Hi,

Those relational operators have operands of all numerical type
int,char,float etc.

They also work for pointer values, PROVIDED both pointers
are NULL or both point into the same array or just after it.
They also are working for character arrays. Whats the logic behind
their working. Is the length of the array compared first and then each
character compared with corresponding character.

{
const char msg[] = "msessage}", ch [] = "Za";
if(msg > ch)

In almost all contexts, using an array name in an
expression produces a value that is a pointer to the array's
first element. `msg' and `ch' in this expression are the
same as `&msg[0]' and `&ch[0]'. These are two pointer values
of the same type, so they can be compared.

... EXCEPT that the comparison in this case invokes
undefined behavior, because the two pointers do not point
to elements of the same array: `msg' and `ch' are different
arrays. Give no credence to the results of your program
after this point; anything can happen.

Greenhorn, several of your questions give the impression
that you are trying to learn C from Usenet. Usenet is a good
source of some kinds of information, but it is not a good
class or a good textbook. I suggest you try one of these,
and then return to Usenet when you need clarification (or
can give clarification!) on a subtle or debatable point;
this is not a good venue for wholesale knowledge transfer.
 
J

Jens.Toerring

Greenhorn said:
Those relational operators have operands of all numerical type
int,char,float etc.
They also are working for character arrays. Whats the logic behind
their working. Is the length of the array compared first and then each
character compared with corresponding character.
{
const char msg[] = "msessage}", ch [] = "Za";
if(msg > ch)
printf("This is working, ch %s is less than msg", ch);
}
whats the morale behind including arrays as valid operators for these
operators?

Because when an array is used in value context (i.e. it is used
as if it had a value like here or e.g. when passed to a function)
it is converted to a pointer to its first element. So what you try
to compare above is pointer to the first elements of the arrays,
i.e. as if you had written

if ( &msg[ 0 ] > &ch[ 0 ] )

Please note that the result of such a comaprison isn't guaranteed
to result in anything "reasonable" - you can only safely compare
pointers for (in)equality unless both point to memory locations
within the same object.
Regards, Jens
 
G

Greenhorn

hi Eric,
thanks for the reply.
the reason i am putting the questions is i am learning C now by reading
Kernighan & Ritchie's "The C programming Language" , second edition.
K&R as many other books seems to first give glimpse of the features and
talk about indepth things later (maybe).
But the problem is when i read the early chapter i get doubts and
sometimes i can't easily figure out where those indepth things are
explained in the book which is when i try to post it to Usenet where i
can find curious and greatly helpful guys like you. The solutioon to
the problem is either get a better book or i should first forget about
my doubts and read the whole book through and re-read it. The latter
option didn't seem to be a better choice so, i am relying on usenet.
Since having a good book saves both your and my time (saving the time
to post and read) i have made a post in pursuit of a book which teaches
C better than K&R , unfortunately with no reply at all.

greenhorn.
 
L

Luke Wu

Greenhorn said:
Those relational operators have operands of all numerical type
int,char,float etc.
They also are working for character arrays. Whats the logic behind
their working. Is the length of the array compared first and then each
character compared with corresponding character.
{
const char msg[] = "msessage}", ch [] = "Za";
if(msg > ch)
printf("This is working, ch %s is less than msg", ch);
}
whats the morale behind including arrays as valid operators for these
operators?

Because when an array is used in value context (i.e. it is used
as if it had a value like here or e.g. when passed to a function)
it is converted to a pointer to its first element. So what you try
to compare above is pointer to the first elements of the arrays,
i.e. as if you had written

if ( &msg[ 0 ] > &ch[ 0 ] )

Please note that the result of such a comaprison isn't guaranteed
to result in anything "reasonable" - you can only safely compare
pointers for (in)equality unless both point to memory locations
within the same object.

Or one "pointer arithmetic step" past the end of the aggregate object.


In other words, > >= < <= should only be used to compare pointer values
(pointer values are really addresses) that "point" into the same
aggregate object or one "pointer arithmetic step" past the end of that
object.

== and != can be used to compare any two pointers (because == returns
false or != returns true when the the pointer values are different,
regardless of where they "point" or even if either or both are NULL).
 
L

Luke Wu

Greenhorn said:
hi Eric,
thanks for the reply.
the reason i am putting the questions is i am learning C now by reading
Kernighan & Ritchie's "The C programming Language" , second edition.
K&R as many other books seems to first give glimpse of the features and
talk about indepth things later (maybe).
But the problem is when i read the early chapter i get doubts and
sometimes i can't easily figure out where those indepth things are
explained in the book which is when i try to post it to Usenet where i
can find curious and greatly helpful guys like you. The solutioon to
the problem is either get a better book or i should first forget about
my doubts and read the whole book through and re-read it. The latter
option didn't seem to be a better choice so, i am relying on usenet.
Since having a good book saves both your and my time (saving the time
to post and read) i have made a post in pursuit of a book which teaches
C better than K&R , unfortunately with no reply at all.

K&R(2) is a great book, and by "better" if you mean "easier for a
beginner" then you have a point. K&R2 requires some programming
experience, although a total beginner can make his/her way through it
with help,but an easier book before this one would make sense.
 
G

Greenhorn

Hi,
K&R2 is a book better than many other books in the market. I am able
to follow the text , but its not very specific on the exact
implementations. For e.g.,in its chapter of prefix and postfix
increment operators (++, --) it doesn't talk about sequence points at
all, which is the key to understand where the values really change.
So, i was thinking a book which explains the language by giving a
glimpse of specifications in the standards would be a better one to
learn from.
greenhorn.
 
M

Mark McIntyre

Hi,
K&R2 is a book better than many other books in the market. I am able
to follow the text , but its not very specific on the exact
implementations. For e.g.,in its chapter of prefix and postfix
increment operators (++, --) it doesn't talk about sequence points at
all, which is the key to understand where the values really change.

For a beginner, sequence points are a little beyond ones need.
So, i was thinking a book which explains the language by giving a
glimpse of specifications in the standards would be a better one to
learn from.

There's a quite comprehensive review of C books at

http://www.accu.org/bookreviews/public/index.htm

and the FAQ for this group lists some recommendations, such as Harbison and
Steele, or King.
 
D

DHOLLINGSWORTH2

C doesn't standardize how the code is generated, only how the language is
interpreted.

You will find that on different hardware, and using different compilers will
give you an assortment of different implimentations.

You will probabky find that on the same compiler, and the same hardware, you
can configure the options to generate a couple of different implementations.

If you are worried about it, tell the compiler to generate opcode, then hand
edit the opcode to do it specifically the way you want it done. Even that
is no garantee.

On the Pentium with pipeline architecture, you may find the instructions are
executed in a different order that you think.

The one garauntee you have is that the Language specifies how the program is
to respond to your code, and the compiler developers take a great deal of
time to consider the Best method for implementing what you want done, and
for what architechture, optimizations etc.

Dan
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top