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.
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.
Avishek Ghosh said:
1 decade ago
At first understand the basic for loop structure how is it works.
for(i=0;i<=5;i++)
{
printf("%d",i); //statement//
}
for(initialization;check condition; updating)
{
statement;
}
It works as follows --1->at first I is assign as 0 // only once //.
2->check the condition.
3-> statement is executed.
4-> increment the value //In updating section//.
5-> check the condition.
6-> the again statement. //Do not go to initialization block//.
Now concentrate about given prob--.
for(i<=5 && i>=-1; ++i; i>0)
printf ("%u, ", i);
So here is works as follows:
1-> when i=0 0<=5 && 0>=-1 //True means 1//.
2-> ++i mean = 2; //Means non zero means true//.
3-> statement;.
4-> 2>0 //True//.
5-> ++i means 3.
6-> statement.
In this procedure it will prints a infinite loops.
for(i=0;i<=5;i++)
{
printf("%d",i); //statement//
}
for(initialization;check condition; updating)
{
statement;
}
It works as follows --1->at first I is assign as 0 // only once //.
2->check the condition.
3-> statement is executed.
4-> increment the value //In updating section//.
5-> check the condition.
6-> the again statement. //Do not go to initialization block//.
Now concentrate about given prob--.
for(i<=5 && i>=-1; ++i; i>0)
printf ("%u, ", i);
So here is works as follows:
1-> when i=0 0<=5 && 0>=-1 //True means 1//.
2-> ++i mean = 2; //Means non zero means true//.
3-> statement;.
4-> 2>0 //True//.
5-> ++i means 3.
6-> statement.
In this procedure it will prints a infinite loops.
(2)
Jose said:
1 decade ago
I've tested this piece of software on Linux with GCC and I agree with Prakhar.
Sai, you can see the output Prakhar is suggesting if you change the output format option from %u to %d. However you're also right about those long numbers representation. I think it happens because the compiler uses a 4-bit equivalent representation for the negative values when you use the %u option and I overflows, but the number of iterations are exactly the same, and when it reaches 0, as Prakhar said, the loop finishes.
Sai, you can see the output Prakhar is suggesting if you change the output format option from %u to %d. However you're also right about those long numbers representation. I think it happens because the compiler uses a 4-bit equivalent representation for the negative values when you use the %u option and I overflows, but the number of iterations are exactly the same, and when it reaches 0, as Prakhar said, the loop finishes.
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);
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);
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.
Shilpa said:
1 decade ago
As said (i<=5 && i>=-1) is for initialization for i=0 is will evaluate to be true.
Then with ++i which is the loop condition i will be incremented to i=1 and printed.
In next iteration (i<=5 && i>=-1) won't be checked since its for initialization which is executed only once.
Hence i will be further incremented i=2 and this value wil be printed. Like this the loop will go on till unsigned integer limit is reached.
Then with ++i which is the loop condition i will be incremented to i=1 and printed.
In next iteration (i<=5 && i>=-1) won't be checked since its for initialization which is executed only once.
Hence i will be further incremented i=2 and this value wil be printed. Like this the loop will go on till unsigned integer limit is reached.
(1)
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.
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.
Sarah said:
1 decade ago
Does this answer mean the i increases from 1 to 32767(0x7fff) and increases again to become -32768(0x1000), and again it becomes -32767(0x1001). By this incremental rule, the last two value is -1(0xffff) and 0(overflow?)
And because it printf as an unsigned int, it shows 1..65535?
I'm confused, can anyone please help me?
And because it printf as an unsigned int, it shows 1..65535?
I'm confused, can anyone please help me?
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.
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