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.

Karthik said:   1 decade ago
@shilpa- You cleared my doubt.

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

Senthil said:   9 years ago
Thank you @Avishek Ghosh.

Arun G Pandian said:   1 decade ago
for loop not understood.

Himanshu said:   1 decade ago
Shilpa is quite right!

Vino said:   1 decade ago
explain for loop....

Elizabeth said:   1 decade ago
limit 65535!! how?

Apple said:   1 decade ago
What is %u for ?

Paridhi said:   1 decade ago
Thanks shilpa!


Post your comments here:

Your comments will be displayed after verification.