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 1 of 5.
Souravb said:
2 decades ago
Can't understand. plz help!!
Zooglaw said:
1 decade ago
It's an endless loop, isn't it? ++i will never break the loop.
Ritu said:
1 decade ago
I can't understand this for loop. Please explain the working method.
Bhuvana said:
1 decade ago
This question confuses the for loop structure. Can you explain me?.
Nandy said:
1 decade ago
@Zooglw: here ++i statement act as conditional statement .it's nt a inc/dec statement.so it has no effect on the for loop
Apple said:
1 decade ago
What is %u for ?
Sundar said:
1 decade ago
%u --> It is to display the data as unsigned integer.
Santosh said:
1 decade ago
++i will break the loop when the value of i is 0.
Arun G Pandian said:
1 decade ago
for loop not understood.
Sundar said:
1 decade ago
I have tested the give program. It prints 1 ... to 65535.
But it takes some time to completely print all values.
But it takes some time to completely print all values.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers