See, following code
if (a = 5 > 4)
;
we know 5 > 4 is True, but True's value is What? Is that 1 ?
See, following code
if (a = 5 > 4)
;
we know 5 > 4 is True, but True's value is What? Is that 1 ?
Can't you simply print out the result?
Code:printf("5 > 4 = %d\n",5 > 4 );
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:
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.Code:#include <stdio.h> #include <ctype.h> int main(void) { printf("%d\n", isupper('A')); }
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.
http://publib.boulder.ibm.com/infoce...e/ref/rele.htmC : 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.
isspace() and so on are different story. Read the doc.
MOREThe values returned are non-zero if the character c falls into the
tested class, and a zero value if not.
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
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.
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,
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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)