From owner-freebsd-hackers Wed Jul 10 22:39:12 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id WAA17565 for hackers-outgoing; Wed, 10 Jul 1996 22:39:12 -0700 (PDT) Received: from seagull.rtd.com (root@seagull.rtd.com [198.102.68.2]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id WAA17538; Wed, 10 Jul 1996 22:38:54 -0700 (PDT) Received: (from dgy@localhost) by seagull.rtd.com (8.7.5/1.2) id WAA17247; Wed, 10 Jul 1996 22:37:58 -0700 (MST) From: Don Yuniskis Message-Id: <199607110537.WAA17247@seagull.rtd.com> Subject: Re: Q: macro expansion To: bde@zeta.org.au (Bruce Evans) Date: Wed, 10 Jul 1996 22:37:58 -0700 (MST) Cc: dgy@rtd.com, freebsd-hackers@freefall.freebsd.org, freebsd-ports@freefall.freebsd.org In-Reply-To: <199607102304.JAA31461@godzilla.zeta.org.au> from "Bruce Evans" at Jul 11, 96 09:04:05 am X-Mailer: ELM [version 2.4 PL24] 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 > > #define X 1 > > #define Y 2 > > X+Y > >I was surprised to see that ``X+Y'' was expanded to ``1 +2''. So, > >I started digging through ANSI and couldn't seem to locate something > >to clearly define this behaviour. > > - why the inserted whitespace? > > See the ISO C standard section 6.8.3 (Macro Replacement). Macros are > expanded as if they were tokenized before expansion (so a `+' at the Ah! Hadn't realized that! ^^^^^^^^^^^^^^^^^^^^^^ > end of X doesn't get joined with the `+' in X+ to form a `++' token). > This is usually implemented by inserting whitespace so that tokenizing > after expansion gives the same result. > > > - why no whitespace after `+'? > > This is a bug in gcc-2.6.3. It actually gives `` 1 +2 '', while Thanx! The inconsistency was what had me most confused! > the version in gcc-2.7.2 gives ``1 + 2 ''. The > difference is important for > > #define Y + > int z; > main() { printf("%d\n", +Y z); } > > For gcc-2.6.3, ``+Y z'' expands to ``++ z'' so there is a bogus `++' > token and the result is 1. For gcc-2.7.2, it expands to ``+ + z'' > and the result is 0. > > Anyway, don't use `gcc -E' as a general purpose macro expander. > `gcc -E -traditional' and /usr/bin/cpp work better. Thanx! --don