C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 6)
6.
What will be the output of the program, if a short int is 2 bytes wide?
#include<stdio.h>
int main()
{
short int i = 0;
for(i<=5 && i>=-1; ++i; i>0)
printf("%u,", i);
return 0;
}
Answer: Option
Explanation:
for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.
In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.
Loop condition always get evaluated to true. Also at this point it increases i by one.
An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)
Discussion:
49 comments Page 5 of 5.
Rejaul said:
9 years ago
Here i<=5 && i >=-1 is not a condition.it's working as a initialise statement.
So ,in the given statements i <= 5 and i >=-1 is true as i = 0, so they both return true,which is 1 and (1 && 1) is 1.
The condition is i ++ .the condition is always true and in each every iteration it will get increased by 1 and the last increment statements has no effect.because the format specifier is unsigned,it will print all possible values.
So ,in the given statements i <= 5 and i >=-1 is true as i = 0, so they both return true,which is 1 and (1 && 1) is 1.
The condition is i ++ .the condition is always true and in each every iteration it will get increased by 1 and the last increment statements has no effect.because the format specifier is unsigned,it will print all possible values.
Senthil said:
9 years ago
Thank you @Avishek Ghosh.
Adil said:
8 years ago
What about the syntax of for loop?
Is this for loop satisfied the syntax of for loop. As for(initialization ; termination condition ; increment / decrement.
Is this for loop satisfied the syntax of for loop. As for(initialization ; termination condition ; increment / decrement.
Stliya said:
8 years ago
@Shilpa.
You are wrong. If it is %d then infinite loop. It is % you so answer option A is correct. Loop terminates at 65535.
You are wrong. If it is %d then infinite loop. It is % you so answer option A is correct. Loop terminates at 65535.
Praveen said:
8 years ago
This doesn't getting terminated even after the value exceeds 65535.
Venkat said:
7 years ago
There format specifier taken as %u so its upto 65535.
If you take %d then its prints -32767 to 32767 so here loop will iterate 65535.
If you take %d then its prints -32767 to 32767 so here loop will iterate 65535.
Sandeep said:
6 years ago
What happens after i value is greater than 65535, is loop terminated or 0 will be printed resulting in an infinite loop.
Supriya said:
5 years ago
I didn't understand.
When i=6 then the condition will false right.
When i=6 then the condition will false right.
(4)
Hemant Pushkarna said:
2 years ago
@All.
Here no use of this condition (i<=5 && i>=-1).
No matter what result this condition gives. Because the loop only depends on i++ & i>0 means the loop will continue till the value of i is greater than 0 & i increment each time by 1.
Here no use of this condition (i<=5 && i>=-1).
No matter what result this condition gives. Because the loop only depends on i++ & i>0 means the loop will continue till the value of i is greater than 0 & i increment each time by 1.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers