From owner-freebsd-hackers Thu May 29 20:13:02 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id UAA25175 for hackers-outgoing; Thu, 29 May 1997 20:13:02 -0700 (PDT) Received: from genesis.atrad.adelaide.edu.au (genesis.atrad.adelaide.edu.au [129.127.96.120]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id UAA25170 for ; Thu, 29 May 1997 20:12:59 -0700 (PDT) Received: (from msmith@localhost) by genesis.atrad.adelaide.edu.au (8.8.5/8.7.3) id MAA23760; Fri, 30 May 1997 12:42:31 +0930 (CST) From: Michael Smith Message-Id: <199705300312.MAA23760@genesis.atrad.adelaide.edu.au> Subject: Re: cc/gcc In-Reply-To: from Steve Howe at "May 29, 97 06:10:28 pm" To: un_x@anchorage.net (Steve Howe) Date: Fri, 30 May 1997 12:42:30 +0930 (CST) Cc: hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-hackers@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk 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 [[