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 3 of 5.
Himanshu said:
1 decade ago
Shilpa is quite right!
Debalipta said:
1 decade ago
Thank you silpa for clearing my doubt.
Rrupinderjit Singh said:
1 decade ago
Here,for loop is equivalent to for( ;i++; ){}.Shilpa's answer is quite explanatory.Many thanks to you.
@Teju--->when function call itself,it is then call as recursive function.And during recursive call, function arguments(or state before call) will be get saved on stack with LIFO accessing logic.
so when fun(--5)==fun(4)) will call itself,argument 4 will get save on stack(at the top of the stack.) Similarly,3 2 1 0 -1.
""4 3 2 1 0 -1""[At stack,with -1 at the top of the stack]
when condition (-1>=0)is checked,then loop will get skipped and -1 will print,as at that time i==-1(due to which loop got skipped).
Now,saved state of recursive function calls will get popped out in LIFO fashion. Since arguments were pushed in order of 4 3 2 1 0 -1.So they will popped out as -1 0 1 2 3 4.
So the answer is -1 -1 0 1 2 3 4.the last SIX values(-1 0 1 2 3 4) are from STACK and first value(-1) is from after condition being checked for last time before popping stack.So do the answer.
HOPE u'll get it.
@Teju--->when function call itself,it is then call as recursive function.And during recursive call, function arguments(or state before call) will be get saved on stack with LIFO accessing logic.
so when fun(--5)==fun(4)) will call itself,argument 4 will get save on stack(at the top of the stack.) Similarly,3 2 1 0 -1.
""4 3 2 1 0 -1""[At stack,with -1 at the top of the stack]
when condition (-1>=0)is checked,then loop will get skipped and -1 will print,as at that time i==-1(due to which loop got skipped).
Now,saved state of recursive function calls will get popped out in LIFO fashion. Since arguments were pushed in order of 4 3 2 1 0 -1.So they will popped out as -1 0 1 2 3 4.
So the answer is -1 -1 0 1 2 3 4.the last SIX values(-1 0 1 2 3 4) are from STACK and first value(-1) is from after condition being checked for last time before popping stack.So do the answer.
HOPE u'll get it.
Jit said:
1 decade ago
@shilpa---you made me out of of the loop!!
Karthik said:
1 decade ago
@shilpa- You cleared my doubt.
Teja said:
1 decade ago
@shilpa great explanation. Now I cleared my doubt.
Vikas said:
1 decade ago
Here i is declared as short int not unsigned int. Is it right that i counts 0 to 65535 instead of -32768 to 32757 ???
Shilpa said:
1 decade ago
Hey everyone don't you think it'll be an infinite loop ? I think its answer is C, because, yes the value will be initialize as one but as the loop goes on when the value of I will be 65535, its condition will be checked that is ++i that will make the I = 0 and so forth the same process will go on and on.
Priya said:
1 decade ago
@shilpa.
When the loop is executed 65534 times, i=65535 but we are having ++i as conditional statement. So now i=0, 0>0 is false so loop is terminated.
When the loop is executed 65534 times, i=65535 but we are having ++i as conditional statement. So now i=0, 0>0 is false so loop is terminated.
Biswajit said:
1 decade ago
@Priya.
You are right.I confuse about initialize part of for loop.
i<=5 && i>=-1 this expression evaluate to 1, but how it is initialize to i, please anyone explain me briefly.
You are right.I confuse about initialize part of for loop.
i<=5 && i>=-1 this expression evaluate to 1, but how it is initialize to i, please anyone explain me briefly.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers