C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d\n", x, y))
    {
        y = x++ <= 5;
    }
    printf("\n");
    return 0;
}
2 1
3 1
4 1
5 1
6 1
7 0
2 1
3 1
4 1
5 1
6 1
2 1
3 1
4 1
5 1
2 2
3 3
4 4
5 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
46 comments Page 1 of 5.

Mohit said:   1 year ago
@Manoj Wagh

Look at the question carefully;

this is x++ not ++x

Manoj wagh said:   2 years ago
#include<stdio.h>
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}
ChatGPT

This C program uses a for loop to iterate through a sequence of values for the variables x and y. Let's break down the code step by step:

Initialization: int x=1, y=1; initializes two integer variables, x, and y, both with a value of 1.

for loop header: for(; y; printf("%d %d\n", x, y)) is a for loop that doesn't have an explicit initialization part (for(;;)). It relies on the initialization done before the loop.

Loop condition: y is the loop condition. The loop will continue as long as y is non-zero.

Loop body: printf("%d %d\n", x, y) prints the current values of x and y to the console.

Inside the loop: y = x++ <= 5; is the statement that updates the value of y in each iteration. It uses the post-increment operator x++, and it sets y to the result of the comparison x <= 5. The loop will continue as long as y is non-zero.

The loop continues until y becomes zero.

After the loop: printf("\n"); prints a newline character, creating a line break.

The program returns 0, indicating successful execution.

Now, let's trace the execution for the first few iterations:

Iteration 1: x=1, y=1 (prints "1 1")
Iteration 2: x=2, y=1 (prints "2 1")
Iteration 3: x=3, y=1 (prints "3 1")
Iteration 4: x=4, y=1 (prints "4 1")
Iteration 5: x=5, y=1 (prints "5 1")
Iteration 6: x=6, y=0 (loop exits as y becomes 0)

So, the output of this program will be:

1 1
2 1
3 1
4 1
5 1
(6)

Kaira said:   6 years ago
Agree @Renuka!

Please, anyone, explain it.
(2)

Andy said:   6 years ago
What is ; for after the bracket, before y?

for(; y; printf("%d %d\n", x, y))
(3)

Rohan said:   7 years ago
int x=1, y=1;

Step 1: initialize the variable x and y to the value 1...

for(; y; printf("%d %d\n", x, y))

Step 2: (i) ; -> it means x=1 [Initialization]
(ii) y; -> condition y=1..
(iii) printf("%d %d\n", x, y) -> prints the value of x and y

y = x++ <= 5;

Step 3:
(i) First time -> y = 1 <=5 , condition true
y = 1 , x = 1+1 =2

(ii) Second time -> y = 2<= 5 , condition true
y =1 , x = 2+1 =3

(iii) Fifth time -> y = 5 < = 5, condition true
y = 1 , x= 5+1 =6


Till now
2 1
3 1
4 1
5 1
6 1


(iv) Last time -> y = 6 <=5 ,condition false
y = 0 , x= 6+1 = 7

Output:-
2 1
3 1
4 1
5 1
6 1
7 0
(27)

Manoja V. said:   8 years ago
Thanks for the explanation @Kiran.
(1)

Vignesh said:   8 years ago
Thanks @Kiran.
(1)

Sahil said:   9 years ago
Before one increment of x, there is one printf statement due to which once x should be printed 1.

Bhargav said:   9 years ago
Thank you all my friends.

Nandini said:   9 years ago
Thanks @Kiran.
(1)


Post your comments here:

Your comments will be displayed after verification.