【问题标题】:Post increment and Pre increment in CC中的后增量和前增量
【发布时间】:2012-06-01 14:42:06
【问题描述】:

我对这两个 C 语句有疑问:

  1. x = y++;

  2. t = *ptr++;

使用语句 1,y 的初始值被复制到 x 中,然后 y 递增。

使用语句 2,我们查看 *ptr 指向的值,将其放入变量 t 中,然后稍后递增 ptr。

对于语句 1,后缀自增运算符的优先级高于赋值运算符。那么不应该先将y递增,然后将x赋值给y的递增值吗?

我不理解这些情况下的运算符优先级。

【问题讨论】:

标签: c post-increment pre-increment operator-precedence


【解决方案1】:

您误解了2] 的含义。后增量总是从增量之前产生值,然后在之后的某个时间增加值。

因此,t = *ptr++ 本质上等价于:

t = *ptr;
ptr = ptr + 1;

这同样适用于您的1] - 从y++ 产生的值是增量之前y 的值。优先级不会改变这一点——无论表达式中其他运算符的优先级高低多少,它产生的值将始终是增量之前的值,并且增量将在之后的某个时间完成。

【讨论】:

  • 检查一下。正如我所说,指针首先递增。 c-faq.com/aryptr/ptrary2.html
  • 看看这个:“后缀++运算符的结果就是操作数的值,得到结果后,操作数的值加一。” (来自 C99 标准的 §6.5.2.4/2)。
  • 你是对的。我以为我可以信任 c-faq ,遵循标准是最好的策略。道歉。
  • @Pkp:我认为您误解了常见问题解答。那里要解决的问题是++ 是否将应用于指针本身,或者指针所指的内容(顺便说一句,这也是在这种情况下确定的优先级)。答案是它适用于指针本身,而不是指针。
【解决方案2】:

C 中前置增量和后置增量的区别:

前置增量和后置增量是内置的Unary Operators。一元的意思是:“一个输入的函数”。 “运算符”的意思是:“对变量进行了修改”。

自增 (++) 和自减 (--) 内置一元运算符会修改它们附加到的变量。如果您尝试对常量或文字使用这些一元运算符,则会收到错误消息。

在 C 中,这里是所有内置一元运算符的列表:

Increment:         ++x, x++
Decrement:         −−x, x−−
Address:           &x
Indirection:       *x
Positive:          +x
Negative:          −x
Ones_complement:  ~x
Logical_negation:  !x
Sizeof:            sizeof x, sizeof(type-name)
Cast:              (type-name) cast-expression

这些内置运算符是变相的函数,它们接受变量输入并将计算结果放回到同一个变量中。

后增量示例:

int x = 0;     //variable x receives the value 0.
int y = 5;     //variable y receives the value 5

x = y++;       //variable x receives the value of y which is 5, then y 
               //is incremented to 6.

//Now x has the value 5 and y has the value 6.
//the ++ to the right of the variable means do the increment after the statement

预增量示例:

int x = 0;     //variable x receives the value 0.
int y = 5;     //variable y receives the value 5

x = ++y;       //variable y is incremented to 6, then variable x receives 
               //the value of y which is 6.

//Now x has the value 6 and y has the value 6.
//the ++ to the left of the variable means do the increment before the statement

后递减示例:

int x = 0;     //variable x receives the value 0.
int y = 5;     //variable y receives the value 5

x = y--;       //variable x receives the value of y which is 5, then y 
               //is decremented to 4.

//Now x has the value 5 and y has the value 4.
//the -- to the right of the variable means do the decrement after the statement

预减量示例:

int x = 0;     //variable x receives the value 0.
int y = 5;     //variable y receives the value 5

x = --y;       //variable y is decremented to 4, then variable x receives 
               //the value of y which is 4.

//x has the value 4 and y has the value 4.
//the -- to the right of the variable means do the decrement before the statement

【讨论】:

    【解决方案3】:
    int rm=10,vivek=10;
    printf("the preincrement value rm++=%d\n",++rmv);//the value is 11
    printf("the postincrement value vivek++=%d",vivek++);//the value is 10
    

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 2012-10-17
      • 2012-03-07
      • 1970-01-01
      • 2014-06-03
      • 2014-12-09
      • 2014-05-03
      • 1970-01-01
      相关资源
      最近更新 更多