A brief description of increment and decrement operators in c.

increment and decrement operators in c

When talking about the operators in C language, we found that C language contains a lot of operators to perform the different task. Generally, every programmer is familiar with the functionality of pre and post increment and decrement operators but I am here to discuss some of the important points and the problem to create a clear visibility for the increment/decrement operators.

Before using the post and pre (increment/decrement) operators, we have to remember that operand should be qualified or unqualified variable or pointer type and should be a modifiable lvalue.



What are the post increment and decrement operators?

When we use post-increment (++) operator on an operand then the result is the value of the operand and after getting the result, the value of the operand is incremented by 1. The working of the post-decrement (–) operator is similar to the post-increment operator but the difference is that the value of the operand is decremented by 1.

Note: incrementation and decrementation by 1 are the types specified.

How are the post-increment and decrement work?

When we apply post-increment or decrement operator on the operand (operand should be lvalue) then the compiler may create a copy of the operand and increment or decrement the value of the operand respectively.

Let’s take an example,

Suppose iData is a signed integer variable having a value of 5. If you apply post-increment on iData than the resultant value of iData will be 5 and the value of iData increment by 1 and becomes 6.

Working of the post-decrement is analogous to the post-increment, except the value of iData that is decremented by 1 and becomes 4.




Which one is better: Pre-increment or Post increment?

Nowadays compiler is enough smart, they optimize the code as per the requirements. The post and pre increment both have own importance we need to use them as per the requirements.

If you are reading a flash memory byte by bytes through the character pointer then here you have to use the post-increment, either you will skip the first byte of the data. Because we already know that in case of pre-increment pointing address will be increment first and after that, you will read the value.

Let’s take an example of the better understanding,

In below example code, I am creating a character array and using the character pointer I want to read the value of the array. But what will happen if I used pre-increment operator? The answer to this question is that ‘A’ will be skipped and  B will be printed.

But in place of pre-increment if we use post-increment then the problem is getting solved and you will get A  as the output.

Besides that, when we need a loop or just only need to increment the operand then pre-increment is far better than post-increment because in case of post increment compiler may have created a copy of old data which takes extra time. This is not 100% true because nowadays compiler is so smart and they are optimizing the code in a way that makes no difference between pre and post-increment. So it is my advice, if post-increment is not necessary then you have to use the pre-increment.

Note: Generally post-increment is used with array subscript and pointers to read the data, otherwise if not necessary then use pre in place of post-increment.Some compiler also mentioned that to avoid to use post-increment in looping condition.
iLoop = 0.

while (a[iLoop ++] != 0)
{
// Body statements
}



What Does “Increment” or “Decrement” Mean in post and pre?

Generally when we apply post or pre-increment/decrement operation at an operand than we think that operand is increased or decreased by 1. Yes, you are right just 1 is added or subtracted from the operand but behavior of this addition and subtraction is different in case of the pointer.

Let’s take an example to understand the above explanation,

I am creating a pointer to integer piData and assigned some address to it.

int *piData = (int *) 0x00000060;

When I am applying post-increment (arithmetic operation on the pointer) on piData (integer pointer) than what happened with the value of piData. Generally, people said that after the operation piData value is 0x00000061, but it is not a right answer if I assumed here integer size is 4 bytes then value piData is 0x00000064 because the compiler knows that 4 bytes are required to store the value of the integer.

Questions on increment and decrement operators in c:

When we have faced the problem related to post and pre increment in c language then we have to remember two important points.

  • The precedence of prefix ++ and * is same with the right to left associativity.
  • The precedence of postfix ++ is higher than the precedence of prefix ++ and * with the left to right associativity.
Question 1:

Output: 101 , 200 , 101

Explanation:

In the above example, two operators are involved and both have the same precedence with a right to left associativity. So the above expression ++*p is equivalent to ++ (*p). In another word, we can say it is pre-increment of value and output is 101, 200, 101.

Question 2:

Output: 100, 200, 200

Explanation:

In the above example, two operators are involved and both have the same precedence with the right to left associativity. So the above expression *++p is equivalent to *(++p). In another word you can say it is pre-increment of address and output is 100, 200,200.

Question 3:

Output: 100, 200, 200

Explanation:

In the above example, two operators are involved and both have the different precedence. The precedence of post ++ is higher than the *, so first post ++ will be executed and above expression, *p++ will be equivalent to *(p++). In another word you can say that it is post increment of address and output is 100, 200, 200.



Some practice questions on increment and decrement operators in c:

Q1:

Q2:

Q3:

Q4:

Your opinion matters

Although here I have tried to discuss a lot of points regarding the post and pre-increment/decrement operators I would like to know your opinion on pre and post operators. So please don’t forget to write a comment in the comment box.



Leave a Reply