Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Jul 1996 09:04:05 +1000
From:      Bruce Evans <bde@zeta.org.au>
To:        dgy@rtd.com, freebsd-hackers@freefall.freebsd.org, freebsd-ports@freefall.freebsd.org
Subject:   Re: Q: macro expansion
Message-ID:  <199607102304.JAA31461@godzilla.zeta.org.au>

next in thread | raw e-mail | index | archive | help
>	#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
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
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.

Bruce



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