In which cases can I ++ and ++ refer to the same value?

Why are i++they the ++isame 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 signal is 6. I found out that the increment operator i++has its current value, i, and causes the value of the stored value to increase i. But the value i'sis displayed as 6, though the current value I is 5. Changing i++to ++ialso give the same value of 6. Why i++and ++ithe same in this case and why the output is equal to 6, although the initial value is 5.

+3
source share
6 answers

The execution order is sequential.

i++ , ++i - , , i , .

printf("%d",i); printf("%d",i++); printf("%d",++i);, -.

EDIT: -, . C C++ lvalue, , , , , i ,

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

(--i)--; // is perfectly legal and works as intended.
+10

, ++ ++?

"++ i, ."

"i ++ i, , , ."

, , .

+1

, .

i++ , .

++I , .

+1

i++ - 1 .

++i - 1 i, .

:

i++ - 5 1 i make i 6. i++, . return 5.

++i - 1 i i 6, i= 6

:

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

:

post : 5, i: 6
pre : 6, i: 6
+1
int i = 5;
i++;   // implies i = i + 1 ==> 6
       // Even ++i results the same          
printf("%d",i); // Obviously it prints 6 
0

If you do not assign a return value to a variable or use it as an argument, the result will be the same.

The main difference between the two is that ++ I increment a variable and only then assigns a value, while ++ I assign and increment first.

0
source

All Articles