-5

I am confused about the output of this program:

#include<stdio.h>
int main()
{
    int i=2;
    while(i + 1?--i:14)
        printf("\n%d", i);

    return 0;
}

The output of the above program is printing 1 and not printing 1 and then 0

When i-- is used instead of --i it will print 1 and then 0

Why is this?

  • why in predecrementation it will print only 1 and not print 0?
  • and why in postdecrementation does it print 1 and then0?
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
user3485153
  • 27
  • 2
  • 5
  • 2
    Do `.` and `?` cost a lot to transmit on the net where you are? – Marc B Oct 28 '14 at 14:19
  • 2
    Why would you need a reason? Just write clearer code that does exactly what you want. All code like this is just obfuscated rubbish and would get you fired in a professional environment. – Martin James Oct 28 '14 at 14:19

2 Answers2

5

This is because i-- results to give the value before the decrement operation will be performed. However, in --i i will be get decremented before its value will be used.

For understanding it better: Lets use your example:

 i=2;

Case 1:

while(i+1?--i:14)

First pass:
Condition i+1?--i:14 will be evaluated as 2+1 ? 1 : 14
This will return 1. The loop will be executed and i will contain the value 1.

Second pass:
Condition i+1?--i:14 will be evaluated as 1+1 ? 0 : 14
This will return 0. Hence loop will not be executed, therefore you are getting output as 1.

case 2:

while(i+1?i--:14)

First pass:
Condition i+1?i--:14 will be evaluated as 2+1 ? 2-- : 14
This will return 2. The loop will be executed and i will contain the value 1.

Second pass:
Condition i+1?i--:14 will be evaluated as 2+1 ? 1-- : 14
This will return 1. The loop will be executed and i will contain the value 0.

The while loop will be executed twice in this case.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
  • but value of i is 2 then i-- must print 2..then 1 and 0..in i-- it will printing only 1 and 0 and in --i it will printing only 1 not printing 0..this is what i am confuse in it.. – user3485153 Oct 28 '14 at 14:22
  • @user3485153 No, because `i--` is done in the loop *condition* so inside the loop `i` is the decreased value. – Some programmer dude Oct 28 '14 at 14:23
  • you mean to say that in while first i-- will decreased that value and then assign to i and then it will decremented? – user3485153 Oct 28 '14 at 14:28
  • 1
    @user3485153 Pre-decrement (e.g. `--i`) is straight forward, it like "decrease `i` then return new value of `i`". Post-decrement (e.g. `i--`) is like "save `i` to a temporary value, decrease `i`, return temporary value (i.e. old value of `i`)". – Some programmer dude Oct 28 '14 at 14:31
  • 2
    Correction: `--i` will *evaluate* to `i-1`, but it's not guaranteed that `i` will be decremented immediately upon evaluation; the only guarantee is that it will be decremented before the next sequence point. This is part of why expressions like `--i * --i` are bad juju. – John Bode Oct 28 '14 at 14:37
2

Lets take this in order:

  1. i == 2, which means that i + 1 is 3 (and non-zero so true) and the loop uses --i for condition which decreases i to 1 which is true and the loop runs

  2. i == 1, which means that i + 1 is 2 (also true), and the loop uses --i for condition which decreases i to zero which is false and the loop exits.


If on the other hand you use the post-decrement its like this:

  1. i == 2, which means that i + 1 is 3 (and non-zero so true) and the loop uses i-- for condition which "returns" 2 (and as it's "true" runs the loop) and then decreases i to 1

  2. i == 1, which means that i + 1 is 2 (and true) and the loop uses i-- which "returns" 1 (and as it's "true" runs the loop) and then decreases i to zero

  3. i == 0, which means that i + 1 is 1 (and true) and the loop uses i-- which "returns" zero (and ends the loop) and then decrease i to -1.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621