473,396 Members | 1,816 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,396 software developers and data experts.

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 1543
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Benson | last post by:
Hi, I've been looking at the "Oracle9 i Database New Features" guide, from which I quote: XML Generation In response to the challenge of generating XML in bulk from a database, XML generation...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
0
by: Pedro Werneck | last post by:
Hi list I'm trying to implement a new type in a C extension and it must support some binary operators, like &, |, ^, << and >>. With &, | and ^, the method must receive another object of the...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
17
by: Peter Bromley | last post by:
The following code snippet does not seem to work correcly unsigned __int64 result = 0xFFFFFFFFFFFFFFFF; result = result << 64; Debugger.WriteLine(System::String::Format(S"{0:X16}",...
10
by: onkar | last post by:
#include<stdio.h> int main(void){ printf("%d %d\n",32<<1,32<<0); printf("%d %d\n",32<<-1,32<<-0); <----------------------------------see here printf("%d %d\n",32>>1,32>>0); printf("%d...
9
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I'm trying to figure out the purpose of these operators. Now, I know what they do. They shift bits either left or right. But I don't know why would anybody want to do that. I've never seen...
14
by: AliceB.Toklas | last post by:
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: ...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.