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);
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development