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

Discussion

What will be the output of the following C code?

#include <stdio.h>
main()
{
    typedef int a;
    a b=2, c=8, d;
    d=(b*2)/2+8;
    printf("%d",d);
}

 

  • A.10
  • B.16
  • C.8
  • D.error

Answer: A

Correct answer: 10

The typedef is a distraction: a is simply another name for int, so b, c and d are ordinary integers.

Precedence settles the arithmetic. * and / bind tighter than + and associate left to right:

\[ (2 \times 2)/2 + 8 = 4/2 + 8 = 2 + 8 = 10 \]

The distractor 16 comes from adding before dividing. Note that c is declared and never used, which is a further piece of misdirection.

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

Post your comment

Please log in or register to post a comment.