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 2 of 5.
Akshay said:
10 years ago
Can anyone explain me why output starts from 1, not from 2 as initialization expression evaluates to 1 and after it, ++i increment i to 2?
Anil said:
1 decade ago
%u returns the address of specified variable here that variable is i.
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)
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?
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.
Prakhar said:
1 decade ago
This option is wrong, actually there is no right option. The code will print first "1" to "32767" after it will increase as usual and print "-32768" to "-1" and now if increase in i++ the value will be "0" so the condition becomes false. Exit the loop.
Sai said:
1 decade ago
If you compile the code in gcc, the output will be different.
After 32767, it is printing from 4294934528 to 4294967295.
After 32767, it is printing from 4294934528 to 4294967295.
Paridhi said:
1 decade ago
Thanks shilpa!
Nidhi said:
1 decade ago
Hey you are right I do not understand how the value is assigned to I. After calculating expression it gives 1 that is fine but there is no assignment is taking place. Please explain@shilpa.
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