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 2 of 5.

Paridhi said:   1 decade ago
Thanks shilpa!

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.

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.

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.

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?

Anil said:   1 decade ago
%u returns the address of specified variable here that variable is i.

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?

Ankit kumar said:   9 years ago
Here is a syntax error, as the place of loop condition and increment have been interchanged.

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.

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


Post your comments here:

Your comments will be displayed after verification.