in Programming in C retagged by
457 views
5 votes
5 votes

What will be the output of the following C program?

#include<stdio.h>
void main()
{
    int i=6;
    for(--i; --i; i--)
    {
      printf("%d",i);
    }
}
  1. $42$
  2. $31$
  3. Infinite loop
  4. None of these
in Programming in C retagged by
457 views

1 Answer

3 votes
3 votes
Initially i = 6

now when entering into the loop first “—i” or pre-decrement of i at the initialization point  makes 6 to 5 and another “—i” which is at  the condition point makes 5 to 4

now when entered into the loop it will give 4 as output.

now after one iteration again one decrement that is post decrement which will make i = 4 to i =3

now again come to the checking point it will make i = 3 to i =2

now enter the loop and print 2

now after the second iteration post decrement will make i = 2 to i =1

and at the checking point as pre decrement is present it will first reduce the value to 0 and the checking condition fails and terminate the loop.

and the final output will be 42.

2 Comments

why i cannot be negative, after 0 can’t i decrement to -1 to -2 and goes till the lowest limit of integer range?
0
0
Because at checking condition it will become 0 and 0 means false so it will get terminated. value other than zero are true in programming
4
4
Answer:

Related questions