Date: Fri, 30 May 1997 08:00:17 -0400 (EDT) From: Thomas David Rivers <ponds!rivers@dg-rtp.dg.com> To: ponds!FreeBSD.ORG!hackers, ponds!anchorage.net!un_x Subject: Re: cc/gcc Message-ID: <199705301200.IAA11334@lakes.water.net>
next in thread | raw e-mail | index | archive | help
>
>
> with cc/gcc, i get outputs of "1" and "0" respectively. why?
> is this construct ABSOLUTELY incorrect, or is something else amuck?
> /*****************************************************************************/
> #include "stdio.h"
> /*****************************************************************************/
> void main (unsigned char argc, unsigned char **argv) {
>
> unsigned char a, b, c;
>
> a = 1; b = 1; c = 0;
> c = a == b == 1 ? 1 : 0 ; printf(" %i\n", c);
> a='1'; b='1'; c = 0;
> c = a == b == '1' ? '1' : '0'; printf(" %c\n", c);
>
> exit( 0 );}
> /*****************************************************************************/
>
>
I believe '?' has a higher precedence than '=='; so you are
really saying:
is b equal to:
if 1 then
1
else
0
compare b and 1 - they are the same.
-> yes
is a equal b?
compare a with the result of b's compare (1 == 1)?
-> yes
assign the 'yes' to c.
c <= 1
Then, in the second case:
> c = a == b == '1' ? '1' : '0'; printf(" %c\n", c);
is b equal to:
if 0x31 then
0x31
else
0x30
compare b and 0x31 - they are the same.
is a iequal to 1 (the result of the b == ..)
-> no ('a' is 0x31, the result of the boolean is 1)
c is assigned the result of the comparison with a and 1
c <= 0
So, c gets the value 0 in the second case.
Does that help?
- Dave Rivers -
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199705301200.IAA11334>
