C语言 前缀和后缀运算符有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7031326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:22:05  来源:igfitidea点击:

What is the difference between prefix and postfix operators?

cpostfix-operatorprefix-operator

提问by pradeep

The following code prints a value of 9. Why? Here return(i++)will return a value of 11 and due to --ithe value should be 10 itself, can anyone explain how this works?

以下代码打印值 9。为什么?这里return(i++)将返回 11 的值,并且由于--i该值本身应该是 10,有人能解释一下这是如何工作的吗?

#include<stdio.h>
main()
{
    int i= fun(10);
    printf("%d\n",--i);
}

int fun (int i)
{
    return(i++);
}

回答by Ken Wayne VanderLinde

There is a bigdifference between postfix and prefix versions of ++.

的后缀版本和前缀版本之间存在很大差异++

In the prefix version (i.e., ++i), the value of iis incremented, and the value of the expression is the newvalue of i.

在前缀版本(即++i)中, 的值i递增,表达式的值为 的i

In the postfix version (i.e., i++), the value of iis incremented, but the value of the expression is the originalvalue of i.

在后缀版本(即i++)中, 的值i递增,但表达式的值是 的原始i

Let's analyze the following code line by line:

让我们一行一行地分析下面的代码:

int i = 10;   // (1)
int j = ++i;  // (2)
int k = i++;  // (3)
  1. iis set to 10(easy).
  2. Two things on this line:
    • iis incremented to 11.
    • The newvalue of iis copied into j. So jnow equals 11.
  3. Two things on this line as well:
    • iis incremented to 12.
    • The originalvalue of i(which is 11) is copied into k. So know equals 11.
  1. i设置为10(简单)。
  2. 这条线上的两件事:
    • i增加到11.
    • i被复制到j. 所以j现在等于11.
  3. 这条线上还有两件事:
    • i增加到12.
    • (即)的原始值被复制到. 所以现在等于.i11kk11

So after running the code, iwill be 12 but both jand kwill be 11.

所以在运行代码后,i将12但双方jk会11。

The same stuff holds for postfix and prefix versions of --.

同样的东西也适用于--.

回答by Dilu Thankachan

Prefix:

字首:

int a=0;

int b=++a;          // b=1,a=1 

before assignment the value of will be incremented.

在赋值之前, 的值会增加。

Postfix:

后缀:

int a=0;
int b=a++;  // a=1,b=0 

first assign the value of 'a' to 'b' then increment the value of 'a'

首先将 'a' 的值赋给 'b' 然后增加 'a' 的值

回答by Dilu Thankachan

The function returns before iis incremented because you are using a post-fix operator (++). At any rate, the increment of iis not global - only to respective function. If you had used a pre-fix operator, it would be 11and then decremented to 10.

i由于您使用的是后缀运算符 (++),因此函数返回之前会增加。无论如何, 的增量i不是全局的 - 仅适用于各自的功能。如果您使用了前缀运算符,它将是11然后递减为10

So you then return ias 10 and decrement it in the printf function, which shows 9not 10as you think.

所以,你再返回i10和printf函数,显示了哪些递减它9并不10像你想象的。

回答by loganfsmyth

In fact return (i++)will only return 10.

实际上return (i++)只会返回 10。

The ++ and -- operators can be placed before or after the variable, with different effects. If they are before, then they will be processed and returned and essentially treated just like (i-1) or (i+1), but if you place the ++ or -- after the i, then the return is essentailly

++ 和 -- 运算符可以放在变量之前或之后,具有不同的效果。如果它们在之前,那么它们将被处理并返回,并且基本上就像 (i-1) 或 (i+1) 一样处理,但是如果您将 ++ 或 -- 放在 i 之后,则返回基本上是

return i;
i + 1;

So it will return 10 and never increment it.

所以它将返回 10 并且永远不会增加它。

回答by Rafe Kettler

The postfix increment ++does not increase the value of its operand until after it has been evaluated. The value of i++is i.

后缀增量++直到它被评估后才增加其操作数的值。值i++IS i

The prefix decrement increases the value of its operand beforeit has been evaluated. The value of --iis i - 1.

前缀递减在计算之前增加其操作数的值。值--iIS i - 1

Prefix increment/decrement change the value before the expression is evaluated. Postfix increment/decrement change the value after.

前缀递增/递减在计算表达式之前更改值。后缀增量/减量更改后的值。

So, in your case, fun(10)returns 10, and printing --iprints i - 1, which is 9.

因此,在您的情况下,fun(10)返回 10,并打印--iprints i - 1,即 9。

回答by Abdelrhman Fawzy

There are two examples illustrates difference

有两个例子说明了差异

int a , b , c = 0 ; 
a = ++c ; 
b = c++ ;
printf (" %d %d %d " , a , b , c++);
  • Here c has value 0 c increment by 1 then assign value 1 to a so value of a = 1and value of c = 1
  • next statement assiagn value of c = 1to b then increment c by 1 so value of b = 1and value of c = 2

  • in printfstatement we have c++this mean that orginal value of c which is 2 will printed then increment c by 1 so printfstatement will print 1 1 2and value of c now is 3

  • 这里 c 的值为 0 c 递增 1 然后将值 1 分配给 so 的值a = 1和值c = 1
  • 下一条语句c = 1将 b 的值分配给 b 然后将 c 增加 1,所以值b = 1和值c = 2

  • printf语句中我们有c++这意味着 c 的原始值是 2 将打印然后将 c 增加 1 所以printf语句将打印1 1 2并且 c 现在的值是 3

you can use http://pythontutor.com/c.html

你可以使用http://pythontutor.com/c.html

int a , b , c = 0 ; 
a = ++c ; 
b = c++ ;
printf (" %d %d %d " , a , b , ++c);
  • Here in printfstatement ++cwill increment value of c by 1 first then assign new value 3 to c so printfstatement will print 1 1 3
  • 这里的printf语句++c将首先将 c 的值增加 1 然后将新值 3 分配给 c 所以printf语句将打印1 1 3

回答by Setu Kumar Basak

Explanation:

解释:

Step 1:int fun(int);Here we declare the prototype of the function fun().

步骤1:int fun(int);在这里我们声明函数的原型fun()

Step 2:int i = fun(10);The variable i is declared as an integer type and the result of the fun(10)will be stored in the variable i.

步骤 2:int i = fun(10);变量 i 被声明为整数类型,并将结果fun(10)存储在变量中i

Step 3:int fun(int i){ return (i++); }Inside the fun()we are returning a value return(i++). It returns 10. because i++is the post-increement operator.

第 3 步:int fun(int i){ return (i++); }在里面fun()我们返回一个值return(i++)。它返回10。因为i++是后递增运算符。

Step 4:Then the control back to the main function and the value 10is assigned to variable i.

第 4 步:然后控制返回主函数并将值10赋给变量i

Step 5:printf("%d\n", --i);Here --idenoted pre-increement. Hence it prints the value 9.

第 5 步:printf("%d\n", --i);这里--i表示预增量。因此它打印 value 9

回答by Ernest Friedman-Hill

First, note that the function parameter named iand the variable named iin main()are two different variables. I think that doesn't matter that much to the present discussion, but it's important to know.

首先,请注意命名的函数参数i和命名变量imain()是两个不同的变量。我认为这对目前的讨论没有那么重要,但重要的是要知道。

Second, you use the postincrement operator in fun(). That means the result of the expression is the value beforeiis incremented; the final value 11 of iis simply discarded, and the function returns 10. The variable iback in main, being a different variable, is assigned the value 10, which you then decrement to get 9.

其次,您在fun(). 这意味着表达式的结果是增加之前的值i;的最终值 11i被简单地丢弃,函数返回 10。imain 中的变量是一个不同的变量,被分配了值 10,然后递减得到 9。

回答by Alok Save

i++ is post increment. The increment takes place after the value is returned.

i++ 是后增量。增量发生在返回值之后。

回答by Kevin

It has to do with the way the post-increment operator works. It returns the value of i and then increments the value.

它与后增量运算符的工作方式有关。它返回 i 的值,然后递增该值。