后增量和pre增量用C [英] Post increment and Pre increment in C

查看:141
本文介绍了后增量和pre增量用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于这两个C语句的问题:

I have a question about these two C statements:


  1. X = Y ++;

T = * PTR ++;

使用说明1,y的初始值被复制成X个则y递增。

With statement 1, the initial value of y is copied into x then y is incremented.

通过声明2,我们期待进入价值所指向* PTR,投入到这变量t,然后晚些时候增加PTR。

With statement 2, We look into the value pointed at by *ptr, putting that into variable t, then sometime later increment ptr.

有关声明1,后缀增量运算具有较高的precedence比赋值操作符。所以不宜ý递增先x被分配给y的递增值α

For statement 1, the suffix increment operator has higher precedence than the assignment operator. So shouldn't y be incremented first and then x is assigned to the incremented value of y?

我不是在这些情况下,运营商的理解precedence。

I'm not understanding operator precedence in these situations.

推荐答案

你是错误的意思你的 2 。后递增总是从增量之前,那么之后的某个时候递增值产生的价值。

You're mistaken about the meaning of your 2]. Post-increment always yields the value from before the increment, then sometime afterward increments the value.

因此​​, T = * PTR ++ 基本上等同于:

Therefore, t = *ptr++ is essentially equivalent to:

t = *ptr;
ptr = ptr + 1;

这同样适用于你的 1] - 从Ÿ++ 产生的值是<$ c的值$ C>是增量之前。 precedence不改变 - 不论多少更高或更低的其他运营商的precedence在恩pression,它产生的值将总是从增量之前的值,而增量将在以后的某个时候完成。

The same applies with your 1] -- the value yielded from y++ is the value of y before the increment. Precedence doesn't change that -- regardless of how much higher or lower the precedence of other operators in the expression, the value it yields will always be the value from before the increment, and the increment will be done sometime afterwards.

这篇关于后增量和pre增量用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆