Dave Lockwood Dave Lockwood - 1 month ago 7
C Question

What would be the output and why?

I was asked in an interview that what would be the Output of the following program::

int main(){
int i=1;
for(i=1;i>=2;i--){
printf("%d",i);
}
}


I ran it in my pc and got no answer.So I wanna know what happened inside the for loop?

Answer

for loops are top-tested, which means the condition (i>=2) is tested before the loop is entered the first time. i is 1, so the condition (i>=2) isn't true, so the loop isn't entered, so nothing is printed.