operands for relational operators <, >, >= etc

Discussion in 'C Programming' started by Greenhorn, Mar 5, 2005.

  1. Greenhorn

    Greenhorn Guest

    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.
     
    Greenhorn, Mar 5, 2005
    #1
    1. Advertising

  2. Greenhorn

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Mar 5, 2005
    #2
    1. Advertising

  3. Greenhorn

    -berlin.de Guest

    Greenhorn <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Mar 5, 2005
    #3
  4. Greenhorn

    Greenhorn Guest

    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.
     
    Greenhorn, Mar 5, 2005
    #4
  5. Greenhorn

    Luke Wu Guest

    -berlin.de wrote:
    > Greenhorn <> 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).
     
    Luke Wu, Mar 5, 2005
    #5
  6. Greenhorn

    Luke Wu Guest

    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.
     
    Luke Wu, Mar 5, 2005
    #6
  7. Greenhorn

    Greenhorn Guest

    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.
     
    Greenhorn, Mar 6, 2005
    #7
  8. On 6 Mar 2005 00:56:20 -0800, in comp.lang.c , "Greenhorn"
    <> 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>
     
    Mark McIntyre, Mar 6, 2005
    #8
  9. 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" <> wrote in message
    news:...
    > On 6 Mar 2005 00:56:20 -0800, in comp.lang.c , "Greenhorn"
    > <> 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>
     
    DHOLLINGSWORTH2, Mar 6, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen herrmannsfeldt

    relational operators on pointers

    glen herrmannsfeldt, Dec 8, 2003, in forum: C Programming
    Replies:
    27
    Views:
    996
    Robert Stankowic
    Dec 19, 2003
  2. Balaji
    Replies:
    9
    Views:
    391
    Balaji
    Jun 16, 2004
  3. Replies:
    4
    Views:
    1,107
    Paul Uiterlinden
    Sep 12, 2006
  4. raju
    Replies:
    49
    Views:
    1,962
    Jordan Abel
    Nov 7, 2005
  5. Kavya
    Replies:
    9
    Views:
    541
    Dik T. Winter
    Oct 28, 2006
Loading...

Share This Page