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.
Vino said:
1 decade ago
explain for loop....
Elizabeth said:
1 decade ago
limit 65535!! how?
Neha said:
1 decade ago
Limit is 65535 because 'u' means unsigned.
Range of short int is 2 byte = 16 bits = -32768 to 32767.
Due to 'u' (unsigned bit) its range become = 65535.
Because unsigned bit doesnt take -ve number as its name shows.
(32767*2)+1 or 32767+32768=65535.
Range of short int is 2 byte = 16 bits = -32768 to 32767.
Due to 'u' (unsigned bit) its range become = 65535.
Because unsigned bit doesnt take -ve number as its name shows.
(32767*2)+1 or 32767+32768=65535.
Nemichand said:
1 decade ago
When the value of i is 6, then how loop work? because statement is
(i<=5 && i>=-1)
Here must be true both condition.
(i<=5 && i>=-1)
Here must be true both condition.
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)
Teju said:
1 decade ago
Will you please explain me following program?
void fact(int n);
void main()
{
int x=5;
fact(x);
getch();
}
void fact(int n)
{
if(n>=0) fact(--n);
printf("%d",n);
}
// Output:
-1-101234
Please explain.
void fact(int n);
void main()
{
int x=5;
fact(x);
getch();
}
void fact(int n)
{
if(n>=0) fact(--n);
printf("%d",n);
}
// Output:
-1-101234
Please explain.
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.
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.
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);
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers