what' value relational operators returned

This is a discussion on what' value relational operators returned within the C Programming forums, part of the General Programming Boards category; See, following code if (a = 5 > 4) ; we know 5 > 4 is True, but True's value ...

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    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,231
    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,231
    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:30 PM
  2. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 06:15 PM
  3. Relational Operators
    By Surfin_Bird in forum C Programming
    Replies: 9
    Last Post: 01-16-2005, 06: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, 03:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21