LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 2

while (n--) – count and final n (output) Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 3, count = 0;
    while (n--)
        count++;
    printf("%d %d\n", count, n);
    return 0;
}
  • A 3 -1 Correct
  • B 3 0
  • C 2 0
  • D 4 -1
Correct Answer: Option A

The condition tests 3, 2, 1 (true) and then 0 (false), decrementing after every test. The body runs 3 times and n ends at −1.

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