C Question

Mike Moroney moroney at jon.DEC
Thu Apr 11 15:19:51 AEST 1985


>An inexperienced C programmer wrote a program containing the following:
>
>	x = x++;
>
>Assuming that x originally had the value 5, what should the value be after
>execution of the above expression ?

The VAX C compiler on VMS sure threw me with its answer to this one. I compiled
and ran a test program and the answer was 5.  Then I looked at the machine
code, and it didn't allocate ANYTHING for x, not even a register!

It generated the following code for the printf statement:

	pushl	#5
	pushal	$CHAR_STRING_CONSTANTS
	calls	#2,printf

  It saw through the whole mess and decided that x was 5 after the "funny"
assignment operation and didn't generate any machine code for the statement
"x = x++;"  I tried more complex programs so the compiler would not know
the value of x beforehand, and it still treated "x = x++;" as a no-op.

"There's a madness to my method."			Mike Moroney
						..decwrl!rhea!jon!moroney



More information about the Comp.lang.c mailing list