469,925 Members | 1,510 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

operands for relational operators <, >, >= etc

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.

Nov 14 '05 #1
8 1374
Greenhorn wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #2
Greenhorn <te************@yahoo.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
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.

Nov 14 '05 #4
Je***********@physik.fu-berlin.de wrote:
Greenhorn <te************@yahoo.com> wrote:
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).

Nov 14 '05 #5
Greenhorn wrote:
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.

Nov 14 '05 #6
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.

Nov 14 '05 #7
On 6 Mar 2005 00:56:20 -0800, in comp.lang.c , "Greenhorn"
<te************@yahoo.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #8
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

"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:12********************************@4ax.com...
On 6 Mar 2005 00:56:20 -0800, in comp.lang.c , "Greenhorn"
<te************@yahoo.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alex Vinokur | last post: by
reply views Thread by Pedro Werneck | last post: by
10 posts views Thread by onkar | last post: by
9 posts views Thread by =?Utf-8?B?QmVu?= | last post: by
14 posts views Thread by AliceB.Toklas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.