Machine-translated by Google. Questions are authored in English.
Top

Discussion

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);
    }

 

  • A.No difference as space doesn’t make any difference, values of a, b, d are same in both the case
  • B.No difference as space doesn’t make any difference, values of a, b, d are different
  • C.Program 1 has syntax error, program 2 is not
  • D.Program 2 has syntax error, program 1 is not

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.