Post increment and Pre increment in C
Asked Answered
M

3

5

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.

Marybellemarybeth answered 26/5, 2012 at 6:38 Comment(1)
You'll probably want to read about sequence points then if this is confusing to you.Smitty
C
6

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.

Carry answered 26/5, 2012 at 6:45 Comment(4)
Chk this out. As I said the pointer gets incremented first. c-faq.com/aryptr/ptrary2.htmlMarybellemarybeth
Check this out: "The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented." (from §6.5.2.4/2 of the C99 standard).Carry
you are correct. I thought I could trust the c-faq , following the standard it the best policy. Apologies.Marybellemarybeth
@Pkp: I think you're misinterpreting the FAQ. The question being addressed there is whether the ++ will apply to the pointer itself, or what the pointer refers to (which, BTW, is also what precedence determines in this case). The answer to that is that it applies to the pointer itself, not the pointee.Carry
I
6

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
Imogeneimojean answered 23/7, 2013 at 19:16 Comment(0)
J
0
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
Jaquelinejaquelyn answered 9/10, 2014 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.