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;
}
1 ... 65535
Expression syntax error
No output
0, 1, 2, 3, 4, 5
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.

Anup said:   1 decade ago
Yeah. It will stop when i reaches its max. value for 16 bits i.e. 65535.

After this the value of i if incremented, turns negative and the loop will exit.

Rahul said:   1 decade ago
@Shilpa you are absolutely correct as bcoz a for loop has a structure as:
for(initilazation;condition;increment or decrement)

So in for loop initilization is checked only once. So in this question just to confuse the order of for loop has been changed.
"i<=5 && i>=-1" this part is provided in initilization part so it just checked only once.

Mano said:   1 decade ago
for(i<=5 && i>=-1; ++i; i>0)

i value is 0

now (0<=5&&0>=-1) the value is 0

after this for ++0 is equal to 1

and then check (1>0)

so we get like this for(0;++0;1>0)
then print i value is 1;

and second time i value is one

so for(0;++1;2>0)

now print i=2

This will be executed 65535. Because unsinged short integer range is 0 to 65535. Why it is called unsinged integer means see this condition (0<=5&&0>=-1);

Aakash said:   1 decade ago
@mano ,,bravo ,but u said (0<=5&&0>=-1 is initialization but how loop can be incremented by i>o

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.

Jit said:   1 decade ago
@shilpa---you made me out of of the loop!!

Souravb said:   2 decades ago
Can't understand. plz help!!


Post your comments here:

Your comments will be displayed after verification.