LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 6

7 / 2 * 2 vs 7 / 2.0 * 2 – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%d %.2f %.2f\n", 7 / 2 * 2, 7 / 2 * 2.0, 7 / 2.0 * 2);
    return 0;
}
  • A 6 6.00 7.00 Correct
  • B 7 7.00 7.00
  • C 6 7.00 7.00
  • D 6 6.00 6.00
Correct Answer: Option A

Evaluation is left to right. In the first two, 7 / 2 is integer division (3) before anything else, so the fraction is already lost. Only 7 / 2.0 divides in floating point.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links