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.
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.
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.
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.
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.
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.
Adil said:
8 years ago
What about the syntax of for loop?
Is this for loop satisfied the syntax of for loop. As for(initialization ; termination condition ; increment / decrement.
Is this for loop satisfied the syntax of for loop. As for(initialization ; termination condition ; increment / decrement.
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.
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.
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?
Venkat said:
7 years ago
There format specifier taken as %u so its upto 65535.
If you take %d then its prints -32767 to 32767 so here loop will iterate 65535.
If you take %d then its prints -32767 to 32767 so here loop will iterate 65535.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers