Pkp
Pkp

Reputation: 969

Post increment and Pre increment in C

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.

Upvotes: 5

Views: 9082

Answers (3)

Eric Leschinski
Eric Leschinski

Reputation: 153812

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

Upvotes: 6

R.M.VIVEK Arni
R.M.VIVEK Arni

Reputation: 11

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

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 489998

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.

Upvotes: 6

Related Questions