Post increment and Pre increment in C

不想你离开。 提交于 2019-11-28 07:03:40

问题


I have a question about these two C statements:

  1. x = y++;

  2. t = *ptr++;

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

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

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?

I'm not understanding operator precedence in these situations.


回答1:


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.

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

t = *ptr;
ptr = ptr + 1;

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.




回答2:


Difference between pre-increment and post-increment in C:

Pre-increment and Post-increment are built-in Unary Operators. Unary means: "A function with ONE input". "Operator" means: "a modification is done to the variable".

The increment (++) and decrement (--) builtin Unary operators modify the variable that they are attached to. If you tried to use these Unary Operators against a constant or a literal, you will get an error.

In C, here is a list of all the Built-in Unary operators:

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

These builtin operators are functions in disguise that take the variable input and place the result of the calculation back out into the same variable.

Example of post-increment:

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

Example of pre-increment:

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

Example of post-decrement:

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

Example of pre-decrement:

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



回答4:


Post increment has lowest precedence of all the operators. Even lower than assignment operator. So when we do p=a++; first value a is assigned to p and the a is incremented hence.



来源:https://stackoverflow.com/questions/10764180/post-increment-and-pre-increment-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!