At 09:55 PM 6/3/2007, Sean Conner wrote:
It was thus said that the Great Chuck Guzis once
stated:
On 3 Jun 2007 at 15:02, davis wrote:
A similar conundrum is:
int f( int, int, int);
n=1;
f(++n, ++n, ++n);
What gets passed to f? Why?
Well, "++n" is more defined than "n++" is, and basically,
pre-increment
MUST happen before the value is used, so in this fragment, n will have
three
distinct values.
When did list this turn into comp.lang.c?
As to what gets passed to f()? That's easy:
n = 1;
f(++n, ++n, ++n);
is the same as
f(4,3,2);
Sorry, that's not correct. Take this program:
#include <stdlib.h>
#include <stdio.h>
void f(int a, int b, int c) {
printf("%d %d %d\n", a, b, c);
}
main (int argc, char * argv[]) {
int n = 1;
f(++n, ++n, ++n);
exit (0);
}
I compiled it using gcc. What comes out?
4 4 4
That's completely consistent with the standard. Any set of three
numbers from 1 to 4 is arguably correct. The only thing that's
guaranteed by "f(++n,++n,++n)" is that after the ";" n is incremented
by 3.
-Rick