programming puzzle (silly)

Tim Olson tim at crackle.amd.com
Sun Mar 12 05:57:01 AEST 1989


In article <YY6_v5y00UkaI0ZW1_ at andrew.cmu.edu> bader+ at andrew.cmu.edu (Miles Bader) writes:
| From klier at ucbarpa.Berkeley.EDU (Pete Klier):
| > 
| > main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;);
| > printf(m?"Answer=%d\n":"error\n",m);}
| > 
| > with help from Jeff Conroy (jmconroy at ernie.Berkeley.EDU)
| 
| The compiler on this machine give the following error; is it wrong?
| 
| E "fact.c",L1/C44:	(syntactic)  unexpected symbol:'*='

Interesting -- the program *is* incorrect, because && has higher
precedence than *= so it is parsed as:

	(n&&m) *= n--

which is illegal because (n&&m) is not an lvalue.  Our MetaWare and
Green Hills compilers catch it, but every pcc-based compiler I tried
compiled it with no warnings.  Looking at the assembly-language
generated, it appears that it compiled as:

	n && (m*=n--)

What do other compilers out there do?


	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list