1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What is printed by printf("%d", 7 / 2); in C when both…

Added 30 Nov -0001

What is printed by printf("%d", 7 / 2); in C when both operands are of type int?

  1. 3.5, because floating division keeps the fraction
  2. 4, because the result is rounded to nearest
  3. 2, because the result is rounded downward
  4. 3, because integer division truncates toward zero Correct answer

Correct answer

D. 3, because integer division truncates toward zero

18 views

MCQSEXAM

Explanation

When both operands of / are integers, C performs integer division and discards the fractional part, truncating toward zero, so 7 / 2 yields 3. No rounding to nearest occurs, and 7 / 2.0 would give 3.5 because one operand is then a double. This truncation is also why the idiom (a + b - 1) / b is used to round a division upward.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs