What will be the output of the following C code?
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i){
printf("%d",i);
if(i==4) break;
}
return 0;
}
.
..
...
....
Explanation:
OUTPUT: 24
Default value of static int variable in C is zero. So,initial value of variable i=0
First iteration:
For loop starts value: ++i i.e. i=0+1=1
For loop condition: ++i i.e i=1+1=2 i.e. loop condition is true
Hence printf statement will print 2
Loop incrimintation: ++i i.e. i=2+1=3
Second iteration:
For loop condition: ++i i.e. i=3+1=4 i.e. loop condition is true.
Hence printf statement will print 4.
Since i is equal to 4, if condition is also satisfies.
But due to break keyword, program control will come out of the for loop.
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i){
printf("%d",i);
if(i==4) break;
}
return 0;
}
.
..
...
....
Explanation:
OUTPUT: 24
Default value of static int variable in C is zero. So,initial value of variable i=0
First iteration:
For loop starts value: ++i i.e. i=0+1=1
For loop condition: ++i i.e i=1+1=2 i.e. loop condition is true
Hence printf statement will print 2
Loop incrimintation: ++i i.e. i=2+1=3
Second iteration:
For loop condition: ++i i.e. i=3+1=4 i.e. loop condition is true.
Hence printf statement will print 4.
Since i is equal to 4, if condition is also satisfies.
But due to break keyword, program control will come out of the for loop.
No comments:
Post a Comment