What is the difference between the following 2 codes?
//Program 1
int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf("%d %d %d", d, a, b);
}
//Program 2
int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf("%d %d %d", d, a, b);
}
Answer: A
Correct answer: No difference as space doesn’t make any difference, values of a, b, d are same in both the case
The two programs are character for character the same, so naturally they produce the same values: a++ contributes 1 and leaves a as 2, ++b makes b 3 and contributes 3, so d is 4 and the output is 4 2 3. The point the question is driving at is that whitespace between tokens is insignificant - though it is worth knowing the limit of that rule: a+++++b would not compile, because the compiler takes the longest possible token at each step and reads it as a++ ++ +b.
Please log in or register to report this question.