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 different tasks. 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 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 a 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.

result = iData++; // apply post increment on iData

         ||
         ||
         \/

result = iData; // old value assigned to the container

iData = iData + 1; //increment iData 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.

result = iData--; // apply post decrement on iData

         ||
         ||
         \/

result = iData; // old value assigned to the container

iData = iData - 1; //decrement iData by 1

 

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

 

What are the pre-increment and decrement operators?

When we use a pre-increment (++) operator then the value of the operand (variable) immediately increases by 1. The result is the value of the (operand+1).

result = ++iData; // apply pre increment on iData

         ||
         ||
         \/
     
iData = iData + 1; //increment iData by 1
     
result = iData; // New value assigned to the container

 

The working of the pre-decrement (–) operator is similar to the pre-increment operator but the difference is that the value of the operand is decremented by 1.

result = --iData; // apply pre decrement on iData

         ||
         ||
         \/
     
iData = iData - 1; //decrement iData by 1
     
result = iData; // New value assigned to the container

 

 

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.

int iData = 5;  //iData is operand
int result = 0;  //container to store the result
result = iData++; // apply post increment on iData


result =>>> iData; // old value assigned to the container
Now
iData += 1; //increment operand by 1

So finally the value of result and iData will be,
result => 5
iData => 6

 

Note: I think Now you able to understand how is the pre-increment and decrement work. So I am not explaining it. If you want its explanation then write in the comment box.

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

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

int iData = 5;  //iData is operand
int result = 0;  //container to store the result
result = iData--; // apply post decrement on iData

result =>>> iData; // old value assigned to the container
Now
iData -= 1; //decremet operand by 1

So finally the value of result and iData will be,
result => 5
iData => 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 their 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 the 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 the 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 a pre-increment operator? The answer to this question is that ‘A’ will be skipped and  B will be printed.

#include <stdio.h>

int main(void)
{

    char acData[5] = {'A','B','C','D','E'};
    char *pcData = NULL;

    pcData = acData;

    printf("%c ",*++pcData);

    return 0;
}

Output: B

 

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.

#include <stdio.h>

int main(void)
{
    char acData[5] = {'A','B','C','D','E'};
    char *pcData = NULL;

    pcData = acData;

    printf("%c ",*pcData++);

    return 0;
}

Output: A

 

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 the 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 the 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 addresses 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 the 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-increment and pre-increment in c language then we have to remember two important points.

  • The precedence of prefix ++ and * is the 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:
#include <stdio.h>

int main(void)
{
    int aiData[5] = {100,200,300,400,500};

    int *piData = aiData;

    ++*piData;

    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);

    return 0;
}

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 compiler looks for associativity and the above expression ++*piData is equivalent to ++ (*piData). In another word, we can say it is pre-increment of value, and output is 101, 200, 101.

 

Question 2:
#include <stdio.h>

int main(void)
{
    int aiData[5] = {100,200,30,40,50};

    int *piData = aiData;

    *++piData;

    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);

    return 0;
}

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 compiler looks for associativity and the above expression *++piData is equivalent to *(++piData ). In another word you can say it is pre-increment of address and output is 100, 200,200.

 

Question 3:
#include <stdio.h>

int main(void)
{
    int aiData[5] = {100,200,30,40,50};

    int *piData = aiData;

    *piData++;

    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);

    return 0;
}

Output: 100, 200, 200

Explanation:

In the above example, two operators are involved and both have different precedence. The precedence of post ++ is higher than the *, so first post ++ will be executed and above expression, *piData++ will be equivalent to *(piData++). 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:

#include <stdio.h>

int main (void)
{

    int i =0;
    int j =0;
    int a[2][3] = {0};
    int (*p)[2] = (int (*)[2])a;

    (*p)[0] = 4;

    p++;
    (*p)[0] = 6;

    ++p;
    (*p)[1] = 27;

    for(i =0; i<2; i++)
    {

        for(j=0; j<3; j++)
        {
            printf("%d ",a[i][j]);
        }

    }

    return 0;
}

Output: ?

 

Q2:

#include <stdio.h>

int main()
{
    int iData[5] = {10,20,30,40,50};
    int *piData = iData;
    int result = 0;

    result = --*piData++;

    printf("%d\n\n", result);

    return 0;
}

Output:  ?

 

Q3:

#include<stdio.h>

int main()
{
    int iData = 100;

    iData++ = 200;

    printf("%d", iData);

    return 0;
}

Output: ?

 

Q4:

#include<stdio.h>

int main()
{
    int iData = 100;

    ++iData = 200;

    printf("%d", iData);

    return 0;
}

Output: ?

 

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.

Recommended Articles for you: