basic question
-
What should the output to the following program be ? VC6 gives: 1 1 #include "stdio.h" void test(int i, int j) { printf("%d %d\n", i, j); } main() { int i = 0; test(i, ++i); }
-
What should the output to the following program be ? VC6 gives: 1 1 #include "stdio.h" void test(int i, int j) { printf("%d %d\n", i, j); } main() { int i = 0; test(i, ++i); }
grr... sorry 'bout that. This message board software is annoying sometimes. ;) Anyway, the order in which function params are evaluated doesn't have to be left-to-right, as demonstrated by your sample code. Since params are pushed on the stack from right to left, the compiler is evaluating the params in right-to-left order, and so "i++" is evaluated first. (You can see this for yourself by looking as the disassembly.) This compiler-reordering is also the reason you must avoid stuff like: int x = i + (i++); There's no guarantee which operand will be evaluated first. --Mike-- ================== The original message was: What should the output to the following program be ?
VC6 gives: 1 1#include "stdio.h"
void test(int i, int j)
{
printf("%d %d\n", i, j);
}main()
{
int i = 0;
test(i, ++i);
}