Thread: what' value relational operators returned

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17

    what' value relational operators returned

    See, following code

    if (a = 5 > 4)
    ;

    we know 5 > 4 is True, but True's value is What? Is that 1 ?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can't you simply print out the result?
    Code:
    printf("5 > 4  = %d\n",5 > 4 );

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    To be fair to the OP, he cannot rely on his compiler's output in this case to know what (if anything) is guaranteed by the standard.

    After all:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
      printf("%d\n", isupper('A'));
    }
    When run on OpenBSD, this prints out 1. Therefore the is* functions return 1 for true, right? Well no, because when I run this on Linux/glibc, I get 256.

    It's true that the relational operators all return 1 for true, but testing on one compiler (or multiple compilers, for that matter) cannot prove that.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    C : The type of the result is int and has the values 1 if the specified relationship is true, and 0 if false. C++: The type of the result is bool and has the values true or false.
    http://publib.boulder.ibm.com/infoce...e/ref/rele.htm

    isspace() and so on are different story. Read the doc.

    The values returned are non-zero if the character c falls into the
    tested class, and a zero value if not.
    MORE

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by cas View Post
    It's true that the relational operators all return 1 for true, but testing on one compiler (or multiple compilers, for that matter) cannot prove that.
    Then ask google!

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17
    Quote Originally Posted by Bayint Naung View Post
    Can't you simply print out the result?
    Code:
    printf("5 > 4  = %d\n",5 > 4 );
    Of corse, but I want a more detailed explanation.

    I know 0 is equivalent True, !0 is False. !0 is what value in relational operators
    compared, which is confuse me indeed.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    zero is false, non zero is true<->false is zero, true is any value except zero

    You sould never rely on non zero value returned from a logical comparison as it can be any value.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    0 is false, anything but 0 is true. True and false in stdbool.h evaluates to 1 and 0 BTW.
    Last edited by Subsonics; 06-15-2010 at 03:16 AM.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17
    Of corse, but I want a more detailed explanation.

    I know !0 is equivalent True, 0 is False. !0 is what value in relational operators
    compared, which is confuse me indeed

    hehe,

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The point is it can be any non-zero value, doesn't matter.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, it is guaranteed that the result of !0 is 1, and that the result of !x, where x is non-zero, is 0. But this is a property of the operator!. If I remember correctly this property applies to the relational operators as well, but I do not have a copy of the C standard with me right now to check. That said, it is true that most of the time you should not be relying on this, but more on a general notion of non-zero as true and zero as false.
    Last edited by laserlight; 06-15-2010 at 04:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 11-30-2008, 11:30 PM
  2. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 07:15 PM
  3. Relational Operators
    By Surfin_Bird in forum C Programming
    Replies: 9
    Last Post: 01-16-2005, 07:45 PM
  4. problem with relational operators
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 09:45 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM