在C运算符关联专门preFIX和后缀递增和递减 [英] Operator associativity in C specifically prefix and postfix increment and decrement

查看:182
本文介绍了在C运算符关联专门preFIX和后缀递增和递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C温度相关性是这样的递增,递减和分配。

In C operation associativity is as such for increment, decrement and assignment.

  2. postfix ++ and -- 
  3. prefix ++ and -- 
  16. Direct assignment = 

的完整列表在这里维基百科的运营商发现,用C

我的问题是,当我们有

int a, b;

b = 1;
a = b++;

printf("%d", a); // a is equal to 1

b = 1;
a = ++b;

printf("%d", a); //a is equal to 2

为什么是等于1与B ++当后缀增量操作者应直接赋值之前发生的呢?

Why is a equal to 1 with b++ when the postfix increment operator should happen before the direct assignment?

为什么是preFIX递增运算符时相比,他们在不同的后缀都分配过吗?

And why is the prefix increment operator different than the postfix when they are both before the assignment?

我是pretty肯定,当涉及到操作的关联,我不明白的东西很重要。

I'm pretty sure I don't understand something very important when it comes to operation associativity.

推荐答案

后缀运算符 A ++ 将递增 A 然后返回即类似于此原始值:

The postfix operator a++ will increment a and then return the original value i.e. similar to this:

{ temp=a; a=a+1; return temp; }

和preFIX ++中的将返回新值即。

{ a=a+1; return a; }

这是风马牛不相及的运营商的 precedence

This is irrelevant to the operator precedence.

(和关联支配是否 ABC 等于(AB)-c A - (BC)

(And associativity governs whether a-b-c equals to (a-b)-c or a-(b-c).)

这篇关于在C运算符关联专门preFIX和后缀递增和递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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