C Programming - Control Instructions - Discussion

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;
}

[A]. 1 ... 65535
[B]. Expression syntax error
[C]. No output
[D]. 0, 1, 2, 3, 4, 5

Answer: Option A

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)


Souravb said: (Aug 1, 2010)  
Can't understand. plz help!!

Zooglaw said: (Sep 22, 2010)  
It's an endless loop, isn't it? ++i will never break the loop.

Ritu said: (Oct 6, 2010)  
I can't understand this for loop. Please explain the working method.

Bhuvana said: (Nov 15, 2010)  
This question confuses the for loop structure. Can you explain me?.

Nandy said: (Jan 18, 2011)  
@Zooglw: here ++i statement act as conditional statement .it's nt a inc/dec statement.so it has no effect on the for loop

Apple said: (Feb 5, 2011)  
What is %u for ?

Sundar said: (Feb 6, 2011)  
%u --> It is to display the data as unsigned integer.

Santosh said: (Feb 15, 2011)  
++i will break the loop when the value of i is 0.

Arun G Pandian said: (Feb 17, 2011)  
for loop not understood.

Sundar said: (Feb 18, 2011)  
I have tested the give program. It prints 1 ... to 65535.

But it takes some time to completely print all values.

Vino said: (Mar 13, 2011)  
explain for loop....

Elizabeth said: (Mar 25, 2011)  
limit 65535!! how?

Neha said: (Jul 2, 2011)  
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.

Nemichand said: (Jul 16, 2011)  
When the value of i is 6, then how loop work? because statement is
(i<=5 && i>=-1)

Here must be true both condition.

Shilpa said: (Aug 19, 2011)  
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.

Teju said: (Aug 23, 2011)  
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.

Anup said: (Aug 31, 2011)  
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.

Rahul said: (Sep 5, 2011)  
@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.

Mano said: (Sep 13, 2011)  
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);

Aakash said: (Sep 30, 2011)  
@mano ,,bravo ,but u said (0<=5&&0>=-1 is initialization but how loop can be incremented by i>o

Himanshu said: (Oct 1, 2011)  
Shilpa is quite right!

Debalipta said: (Nov 23, 2011)  
Thank you silpa for clearing my doubt.

Rrupinderjit Singh said: (Dec 22, 2011)  
Here,for loop is equivalent to for( ;i++; ){}.Shilpa's answer is quite explanatory.Many thanks to you.

@Teju--->when function call itself,it is then call as recursive function.And during recursive call, function arguments(or state before call) will be get saved on stack with LIFO accessing logic.

so when fun(--5)==fun(4)) will call itself,argument 4 will get save on stack(at the top of the stack.) Similarly,3 2 1 0 -1.

""4 3 2 1 0 -1""[At stack,with -1 at the top of the stack]

when condition (-1>=0)is checked,then loop will get skipped and -1 will print,as at that time i==-1(due to which loop got skipped).

Now,saved state of recursive function calls will get popped out in LIFO fashion. Since arguments were pushed in order of 4 3 2 1 0 -1.So they will popped out as -1 0 1 2 3 4.


So the answer is -1 -1 0 1 2 3 4.the last SIX values(-1 0 1 2 3 4) are from STACK and first value(-1) is from after condition being checked for last time before popping stack.So do the answer.

HOPE u'll get it.

Jit said: (Dec 23, 2011)  
@shilpa---you made me out of of the loop!!

Karthik said: (Jan 1, 2012)  
@shilpa- You cleared my doubt.

Teja said: (Jan 31, 2012)  
@shilpa great explanation. Now I cleared my doubt.

Vikas said: (Mar 24, 2012)  
Here i is declared as short int not unsigned int. Is it right that i counts 0 to 65535 instead of -32768 to 32757 ???

Shilpa said: (Apr 10, 2012)  
Hey everyone don't you think it'll be an infinite loop ? I think its answer is C, because, yes the value will be initialize as one but as the loop goes on when the value of I will be 65535, its condition will be checked that is ++i that will make the I = 0 and so forth the same process will go on and on.

Priya said: (Jun 22, 2012)  
@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.

Biswajit said: (Jul 22, 2012)  
@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.

Nidhi said: (Aug 25, 2012)  
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.

Paridhi said: (Aug 28, 2012)  
Thanks shilpa!

Sai said: (Dec 24, 2012)  
If you compile the code in gcc, the output will be different.
After 32767, it is printing from 4294934528 to 4294967295.

Prakhar said: (Jan 27, 2013)  
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: (Jun 19, 2013)  
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: (Jul 4, 2014)  
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?

Avishek Ghosh said: (Jun 27, 2015)  
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.

Anil said: (Jul 9, 2015)  
%u returns the address of specified variable here that variable is i.

Akshay said: (Nov 8, 2015)  
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: (Jul 20, 2016)  
Here is a syntax error, as the place of loop condition and increment have been interchanged.

Rejaul said: (Sep 23, 2016)  
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: (Feb 19, 2017)  
Thank you @Avishek Ghosh.

Adil said: (May 24, 2017)  
What about the syntax of for loop?

Is this for loop satisfied the syntax of for loop. As for(initialization ; termination condition ; increment / decrement.

Stliya said: (Jun 7, 2017)  
@Shilpa.

You are wrong. If it is %d then infinite loop. It is % you so answer option A is correct. Loop terminates at 65535.

Praveen said: (Oct 9, 2017)  
This doesn't getting terminated even after the value exceeds 65535.

Venkat said: (Mar 3, 2018)  
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.

Sandeep said: (Oct 15, 2019)  
What happens after i value is greater than 65535, is loop terminated or 0 will be printed resulting in an infinite loop.

Supriya said: (Jun 10, 2020)  
I didn't understand.

When i=6 then the condition will false right.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.