Pre-increment and Post-increment in C/c++

Pre-increment and Post-increment in C/C++

In this blog post, we will learn pre-increment and post-increment in C/C++ with some example programs. We will also see what is the difference between pre-increment and post-increment operators and why in c++ pre-increment return lvalue and in C rvalue.

 

What is a pre-increment operator?

A pre-increment operator (++)  is used to increment the value of an operand (variable) before using it in an expression. It means when we use a pre-increment (++) operator then the value of the operand (variable) increases immediately 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

Note: The result is the new value of the operand after incrementation. The expression++E is equivalent to (E+=1).

 

How is the pre-increment work?

When we apply a pre-increment operator on the operand (operand should be lvalue) then the value of the operand (variable) increases immediately by 1.

Let see an example to understand the concept,

Suppose data is an integer variable and its value is 10. If you use a pre-increment operator on data than the value of data will be 11 and the value of the result will be also 11.

#include <stdio.h>

int main(void)
{
    int data = 10;
    int result = 0;

    result = ++data;

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

    return 0;
}

Output:

data = 11
result = 11

 

Explanation of the example code,

int data = 10;  //Data is operand
int result = 0;  //Container to store the result

result = ++data; //Apply pre increment on data

Now,
data += 1; //Increment operand by 1
result =>>> data; //New value assigned to the container

So finally the value of result and data will be,
result => 11
data => 11

 

 

What is a post-increment operator?

A post-increment operator (++)  is used to increment the value of an operand (variable) after executing expression completely in which post-increment is used. It means when we use a post-increment (++) operator then the value of the operand (variable) is used first after that it incremented.

result = iData++; // apply post increment on iData
         ||
         ||
         \/
result = iData; // old value assigned to the container
iData = iData + 1; //increment iData by 1

 

Note: The result of the postfix++ operator is the value of the operand.

 

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 data is an integer variable and its value is 10. If you use a post-increment operator on data than the value of the result is  10 and the value of the data incremented by 1 and becomes 11.

#include <stdio.h>

int main(void)
{
    int data = 10;
    int result = 0;

    result = data++;

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

    return 0;
}

Output:

data = 11
result = 10

 

Explanation of the example code,

int iData = 10;  //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 => 11
iData => 0

 

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 before reading 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
}

 

Evaluating post-increment and pre-increment together:

  • Precedence of postfix ++ is higher than prefix ++ and their associativity is also different.
  • Associativity of postfix ++ is left to right.
  • Associativity of prefix ++ is right to left.
  • The precedence of prefix ++ and * is the same with the right to left associativity.
  • Precedence of postfix ++ is higher than * and their associativity is also different.

 

In C language, Pre-increment, and post-increment operators can not be used as an lvalue. Let see some examples to understand this concept.

#include<stdio.h>

int main()
{
    int data = 6;

    data++ = 27;

    printf("data = %d", data);

    return 0;
}

Output:

Error: lvalue required as left operand of assignment

 

#include<stdio.h>

int main()
{
    int data = 6;

    ++data = 27;

    printf("data = %d", data);

    return 0;
}

Output:

Error: lvalue required as left operand of assignment

 

 

But in C++,  pre-increment can be used as an lvalue and post-increment can not be used as an lvalue. Let’s compile the above code using the C++ compiler.

#include <cstdio> 
int main()
{
    int data = 6;

    ++data = 27;

    printf("data = %d", data);

    return 0;
}

Output:

data = 27

 

#include <cstdio>
int main()
{
    int data = 6;

    data++ = 27;

    printf("data = %d", data);

    return 0;
}

Output:

Error: lvalue required as left operand of assignment

 

Why pre-increment operator gives rvalue in C but in C++ lvalue?

C doesn’t have references other hand C++ has references. In C++,  ++data returns a reference to data (lvalue) whereas in C it returns an incremented value of data. The ++data could also be assigned to a reference as follows:

int &ref = ++data; // valid

 

 

Recommended Articles for you: