1,221 views
0 votes
0 votes

Find output
 

#include<stdio.h>
void main()
{
    
int i=0;

for(; i++ ; printf("%d",i));

printf("%d",i);

}


Plz explain ?

4 Answers

6 votes
6 votes
In condition part of for loop, i++ evaluates to 0, and thus control doesn't go into for loop.

So it goes out of for loop and prints value of i, which is 1 now.

So answer is (C) 1.
0 votes
0 votes
ans is D .

since i is already initialised to 0

condition is i++ which is always true

then control goes to body of loop & print 1(since i has been incremented)

then again i will be printed once since in the place of increment/ decrement operator  printf("%d",i) is given

so atleast   11 will be printed in the first run

and loop will continue since the condition is always true upto range of integer
0 votes
0 votes

 

There two points need to notice here before answer the question.

Point 1. End of the FOR loop

look at carefully for loop in above program 

for(;i++;printf("%d",i)); 

semicolon at end of the loop,  it same as (nothing inside the for loop code block)

for(;i++;printf("%d",i)

{

//nothing 
}

it's clear that 

print("%d",i) at 6th line is not part of the FOR loop code.

-------------------------------------------------------------------------------------------------------------------

Point 2 : FOR loop syntax 

for (initialization statement; test expression; update statement) {
 // statements
}

in above code for (    ;   i++  ; prinft("%d",i))

initialization statement =  

test expression= i++

update statement= printf("%d",i)

How FOR loop execute the flow  ?

Now let's crack the above program.

Init : before FOR loop , i value is already assigned to 0 (i=0)

Next , Condition check : Test Expression is i++

          i++ is postfix increment , so value is of i is zero (i=0 while evaluating the expression) but increment will happen  after condition check.

        Expression check in for loop is simple as if(Expression)

       if(0) -> return false ,thus control doesn't go into for loop.

 

Now program control out of for loop and value of i will be 1 (i=1, because of postfix increment).

Next statement is line 6, Prinft("%d",i);

So, output is 1

 

 

          

 

–1 votes
–1 votes
The answer is 1. that is option (c). Explanation: The for loop has a semicolon. It executes only once. The working of for loop is first initialization (which is not there here (;) ) then the check (i++ ) So i is incremented. the for loop control ends . it doesnot print. then the next print will come.

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked Apr 16
70 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
116 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
254 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how