C question
DAPKUS
dad at aluxp.UUCP
Sun Apr 7 08:15:09 AEST 1985
> We've got a raging controversy going on here at Tektronix, and I thought
> that I would appeal to you for an 'official' opinion.
>
> 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 ?
> ...
> I would like to point out that the various compilers around here have been
> tried, and we have 1 vote for 6 (our 4.2BSD compiler), and 3 votes for 5
> (various microprocessor compilers).
> ...
> Paul Hoefling (...!tektronix!paulh)
I am fairly new to C, but by some random experimenting I witnessed a
strange Phenomenon. The following program outputs a "5" on a VAX under SVR2:
main () {
int x;
x = 5;
x = x++;
printf ("%d\n", x);
}
The active part of the generated assembly code was:
movl $5,-4(fp)
movl -4(fp),r0
incl -4(fp)
movl r0,-4(fp)
The following code on the same system outputs 6. The
only difference is that x is made a register-class:
main () {
register int x;
x = 5;
x = x++;
printf ("%d\n", x);
}
That generates:
movl $5,r11
movl r11,r11
incl r11
Doesn't it seem slightly paradoxical and dangerous that using "register"
in this case alters the output of the program so entirely? Also how
about that redundant "movl r11,r11" generated in the register version?
Donald A. Dapkus
aluxp!dad
More information about the Comp.lang.c
mailing list