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

Supriya said:   5 years ago
I didn't understand.

When i=6 then the condition will false right.
(4)

Avishek Ghosh said:   1 decade ago
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.
(2)

Hemant Pushkarna said:   2 years ago
@All.

Here no use of this condition (i<=5 && i>=-1).
No matter what result this condition gives. Because the loop only depends on i++ & i>0 means the loop will continue till the value of i is greater than 0 & i increment each time by 1.
(1)

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.
(1)

Vikas said:   1 decade ago
Here i is declared as short int not unsigned int. Is it right that i counts 0 to 65535 instead of -32768 to 32757 ???

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

Shilpa said:   1 decade ago
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:   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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.