为什么后缀的优先级高于前缀?
Why postfixed has higher precedence than prefixed?
案例 1:
int i = 10;
int j = i++;
这里先把i的值赋给j,然后i加1。
案例二:
int i = 10;
int j = ++i;
这里先把i加1赋值给j
那么,如果前缀形式先进行自增操作,那为什么后缀比前缀高呢?
这与优先级无关。(此处)在预增量中,分配的值是副作用发生之前的值。
对于预增量,它将是副作用后的值。
int i = 10;
int j = i++;
在 i
的递增值之前是 10。因此 j=10
在执行这 2 个语句之后。
int i = 10;
int j = ++i;
这里的值会是11。因为incremenet是先完成后赋值的。
Linux manual
operator
页定义为 same priority
pre/post increment
.
! ~ ++ -- + - (type) * & sizeof right to left
post增量规则先赋值再增量
int j = i++;// here first whatever i value is there that will be assigned to j
预自增规则是先自增再赋值
int j = ++i;//++i itself will change i value & then modfied value will assign to j.
例如考虑下面的例子
#include<stdio.h>
int main()
{
int x = 10;
int *p = &x;// assume p holds 0x100
printf("x = %d *p = %d p = %p\n",x,*p,p);
++*p++;
/** in above statement ++ and * having same precedence,then you should check assocaitivity which is R to L
So start solving from Right to Left, whichever operator came first , solve that one first
first *p++ came , again solve p++ first, which is post increment,so address will be same
*0x100 means = 10
now ++10 means = 11
**/
printf("x = %d *p = %d p = %p\n",x,*p,p);
}
案例 1:
int i = 10;
int j = i++;
这里先把i的值赋给j,然后i加1。
案例二:
int i = 10;
int j = ++i;
这里先把i加1赋值给j
那么,如果前缀形式先进行自增操作,那为什么后缀比前缀高呢?
这与优先级无关。(此处)在预增量中,分配的值是副作用发生之前的值。 对于预增量,它将是副作用后的值。
int i = 10;
int j = i++;
在 i
的递增值之前是 10。因此 j=10
在执行这 2 个语句之后。
int i = 10;
int j = ++i;
这里的值会是11。因为incremenet是先完成后赋值的。
Linux manual
operator
页定义为 same priority
pre/post increment
.
! ~ ++ -- + - (type) * & sizeof right to left
post增量规则先赋值再增量
int j = i++;// here first whatever i value is there that will be assigned to j
预自增规则是先自增再赋值
int j = ++i;//++i itself will change i value & then modfied value will assign to j.
例如考虑下面的例子
#include<stdio.h>
int main()
{
int x = 10;
int *p = &x;// assume p holds 0x100
printf("x = %d *p = %d p = %p\n",x,*p,p);
++*p++;
/** in above statement ++ and * having same precedence,then you should check assocaitivity which is R to L
So start solving from Right to Left, whichever operator came first , solve that one first
first *p++ came , again solve p++ first, which is post increment,so address will be same
*0x100 means = 10
now ++10 means = 11
**/
printf("x = %d *p = %d p = %p\n",x,*p,p);
}