在什么情况下,i++ ++ +i可以表示相同的值?(复制)

时间:2022-06-29 09:05:47

This question already has an answer here:

这个问题已经有了答案:

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

为什么在下面的代码中,i++和+i是相同的?

#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.

输出是6。我了解到增量运算符I ++有其值I的当前值,并使I的存储值递增。但是i的值显示为6,而i的当前值是5。将i++ +替换为++i也会得到相同的值6。为什么i++和+i在这种情况下是一样的,为什么输出是6虽然初始值是5。

6 个解决方案

#1


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.

i++ +i是在那个序列点上执行的一条指令,而i的值在那个指令的任何地方都没有被使用,所以这并不重要。

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

如果替换printf(“%d”,i);与printf(" % d”,我+ +);或printf(" % d”,+ + i);事情将会大不相同。

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

编辑:我还发现了一些非常有用的知识。在C和c++中,前缀unary操作符返回一个lvalue,与后缀unary运算符形成对比,因此,如果您想要,例如,递减i两次。

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

whereas

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

#2


1  

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

看看我在+ I和I ++之间的区别是什么?

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

“++i将增加i的值,然后返回递增值。”

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

“i++ +将增加i的值,但返回我在增加之前保持的原始值。”

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

你不使用它返回的值,所以它在你的情况下不重要。

#3


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.

使用i++你将在方法中使用i,一旦完成,我将增加。

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

使用++I首先增加值,然后在方法中使用它。

#4


1  

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

i++ -添加1,返回旧值。

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

+i -给i加上1,返回新的值。

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++ -返回5,加1使i为6。如果你得到了i++的返回值,你就能得到一个清晰的概念。因为返回值是5。

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

+i -给i加上1,设i为6,然后返回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

#5


0  

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

#6


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.

两者之间的主要区别是++i增加变量,然后才分配值,而i++ +先分配值,然后再增加值。

#1


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.

i++ +i是在那个序列点上执行的一条指令,而i的值在那个指令的任何地方都没有被使用,所以这并不重要。

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

如果替换printf(“%d”,i);与printf(" % d”,我+ +);或printf(" % d”,+ + i);事情将会大不相同。

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

编辑:我还发现了一些非常有用的知识。在C和c++中,前缀unary操作符返回一个lvalue,与后缀unary运算符形成对比,因此,如果您想要,例如,递减i两次。

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

whereas

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

#2


1  

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

看看我在+ I和I ++之间的区别是什么?

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

“++i将增加i的值,然后返回递增值。”

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

“i++ +将增加i的值,但返回我在增加之前保持的原始值。”

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

你不使用它返回的值,所以它在你的情况下不重要。

#3


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.

使用i++你将在方法中使用i,一旦完成,我将增加。

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

使用++I首先增加值,然后在方法中使用它。

#4


1  

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

i++ -添加1,返回旧值。

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

+i -给i加上1,返回新的值。

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++ -返回5,加1使i为6。如果你得到了i++的返回值,你就能得到一个清晰的概念。因为返回值是5。

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

+i -给i加上1,设i为6,然后返回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

#5


0  

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

#6


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.

两者之间的主要区别是++i增加变量,然后才分配值,而i++ +先分配值,然后再增加值。