C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 14)
14.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i = 5;
    while(i-- >= 0)
        printf("%d,", i);
    i = 5;
    printf("\n");
    while(i-- >= 0)
        printf("%i,", i);
    while(i-- >= 0)
        printf("%d,", i);
    return 0;
}
4, 3, 2, 1, 0, -1
4, 3, 2, 1, 0, -1
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
Error
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
Answer: Option
Explanation:

Step 1: Initially the value of variable i is '5'.
Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1

Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.

Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1

Discussion:
11 comments Page 1 of 2.

Vikas Potdar said:   3 years ago
I think the answer should be option C.

Ashu said:   6 years ago
After the first loop, the value of i is assigned 5 then why i=4 is used in the second printf?

Chintu kumar said:   9 years ago
In first loop id absolutely correct because 'i' of value is posted prefix but the value of 'i' is change because the only comparison, not to change the value. But this time is change and the value of 'i' = 4 in the first loop.

VARUN said:   1 decade ago
From operator precedence, we know that unary operator post-decrement has higher priority than comparison operator.

But due to post-decrement property, the value of i will be decremented only after it had been used for comparison.

So at the first iteration, the condition is true because 5>=0 and then i is decremented. Therefore 4 will be printed.

Similarly the loop continues and the value of i keeps on decrementing.

Let us see what what happen when condition of while loop becomes 0 >= 0. At this time, condition is met and i is decremented.

Chetana said:   1 decade ago
In the first ( hile i-- >= 5)
printf("/n %d/",i)

Why does it print i = 4 ? why not i = 5 ? in post decrement, at first time use, the value is unchanged, but in second use it's decremented, but here its decrementing in first use itself .
(1)

Sandy said:   1 decade ago
After the first loop we again giving the value of i is 5. So why we take the value of i as 4 in second loop.

Anusha said:   1 decade ago
When the post decrement or increment is used in the while statement, first the decrement is performed and then it is checked with zero. Hence the value of i is 4 in first use itself.

It is not in the use of first time. The post decrement means it will be decremented after the execution of that particular instruction.

Teja said:   1 decade ago
Hi friends.
%i is a format specifier for integer same as %d.
In above we are using a post increament so the value printed from 4.

Anish said:   1 decade ago
Printf ("%i, ", i) ;.

How cum integer value of I is printed if %i is declared. ?

Ramarao said:   1 decade ago
Here in second while %i is given instead of %d.


Post your comments here:

Your comments will be displayed after verification.