Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 30 May 1997 12:42:30 +0930 (CST)
From:      Michael Smith <msmith@atrad.adelaide.edu.au>
To:        un_x@anchorage.net (Steve Howe)
Cc:        hackers@FreeBSD.ORG
Subject:   Re: cc/gcc
Message-ID:  <199705300312.MAA23760@genesis.atrad.adelaide.edu.au>
In-Reply-To: <Pine.BSF.3.95q.970529180401.8774A-100000@aak.anchorage.net> from Steve Howe at "May 29, 97 06:10:28 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
Steve Howe stands accused of saying:
> 
> with cc/gcc, i get outputs of "1" and "0" respectively.  why?
> is this construct ABSOLUTELY incorrect, or is something else amuck?

Well, apart from the fact that not using parentheses places you at the
mercy of precedence that you may not appreciate (see
/usr/share/misc/operator), let's have a look :

> /*****************************************************************************/
> #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);

No need to initialise c.
== groups left-to-right, has highest precedence.
 a == b -> true
 true == 1 -> true
 true ? 1 : 0 -> 1
 c = 1

>      a='1'; b='1'; c = 0;
>      c = a == b == '1' ? '1' : '0'; printf(" %c\n", c);

Likewise no need to initialise c.
 a == b -> true
 true == '1' -> false  ***
 false ? '1' : '0' -> '0'
 c = '0'


It is generally unwise to try to cast a boolean (truth) value to a 
scalar in any context.

-- 
]] Mike Smith, Software Engineer        msmith@gsoft.com.au             [[
]] Genesis Software                     genesis@gsoft.com.au            [[
]] High-speed data acquisition and      (GSM mobile)     0411-222-496   [[
]] realtime instrument control.         (ph)          +61-8-8267-3493   [[
]] Unix hardware collector.             "Where are your PEZ?" The Tick  [[



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199705300312.MAA23760>