3

Why is i++ and ++i same in the following code?

#include <stdio.h>  

int main()  
{  
    int i=5;  

    while(1)  
    {  
        i++;                  /*replacing i++ by ++i also gives 6*/  
        printf("%d",i);  
        break;  
    }  

    return 0; 
}  

The output is 6. I learnt that the increment operator i++ has its value the current value of i and causes the stored value of i to be incremented.But i's value is displayed as 6 though the current value of i is 5. Replacing i++ by ++i also gives the same value 6. Why is i++ and ++i same in this case and why output is 6 though initial value is 5.

Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
user3124361
  • 471
  • 5
  • 21
  • See also [Pre and post increment in programming](https://stackoverflow.com/questions/16733015/pre-and-post-increment-in-programming) and [Where can I find the implementation for the ++ operator?](https://stackoverflow.com/questions/14169145/where-can-i-find-the-implementation-for-the-operator/14169214#14169214) – Cilyan Feb 06 '14 at 10:58

6 Answers6

10

The order of execution is sequential.

i++ or for that matter ++i is a single instruction to be executed at that sequence point, with i's value not being used anywhere at that instruction, so it doesn't really matter.

If you do replace printf("%d",i); with printf("%d",i++); or printf("%d",++i); things will be much different.

EDIT: I also discovered something that is fairly useful to know. In C and C++, the prefix unary operator returns an lvalue, in contrast to the postfix unary operator, so if you want to, for example, decrement i twice, then

(i--)--; // is illegal

whereas

(--i)--; // is perfectly legal and works as intended.
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
1

Check out the answer I found at What is the difference between ++i and i++?

"++i will increment the value of i, and then return the incremented value."

"i++ will increment the value of i, but return the original value that i held before being incremented."

You don't use value which it returns, so it does not matter in your case.

Community
  • 1
  • 1
1

It only changes what value will be set when used in a method.

With i++ you'll use i in the method, once done i will be increased.

With ++I first you increase the value and then you use it in the method.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
1

i++ - add 1 to i returns the old value.

++i - add 1 to i, returns the new value.

In your case :

i++ - returns 5 and add 1 to i make i as 6. If you catch the return value of i++ you can get the clear idea. because return will have the value 5.

++i - add 1 to i and make i as 6 then return i=6

Sample code:

#include <stdio.h>  
int main()  
{  
    int i=5;  
    while(1)  
    {  
        int post, pre;
        post = i++;  
        printf("post : %d, i: %d\n", post,  i);  
        
        i = 5;
        pre = ++i;
        printf("pre : %d, i: %d\n", pre,  i);  
        break;  
    }  
    return 0; 
}  

Output:

post : 5, i: 6
pre : 6, i: 6
Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33
0
int i = 5;
i++;   // implies i = i + 1 ==> 6
       // Even ++i results the same          
printf("%d",i); // Obviously it prints 6 
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0

If you don't assign the returned value to a variable or use it as an argument, the result is the exact same.

The primary difference between the two is that ++i increments the variable and only then assigns the value, while i++ assigns first and increments afterwards.

Veselin Romić
  • 703
  • 6
  • 11