In-Depth Analysis: Post-Increment vs Pre-Increment Operators

The reason for writing this blog post was a time when I encountered a problem with my code and couldn't debug it. It was one of those days when you start to question whether you have what it takes to be a "real" developer. Everything seemed fine, but the code wouldn't increment as expected...

Every 144 lines of code has an increment or decrement operation

-Patrick Okafor

In software development, it's common to seek new and shorter ways to solve old problems and improve existing solutions. An in-depth analysis of increment and decrement operations has confirmed this tendency. Over time, it has been observed that the three conventional ways of decrementing/incrementing operations tend to become shorter and more readable than their predecessors, leading to better code readability overall.

x = x + 1; //  1. increment x by 1
x += 1;    //  2. increment x by 1
++x;       //  3. increment x by 1

Suppose we have a variable x assigned a value of 5. This statement x = x + 1 adds 1 to x. The new value of x becomes 6.

#include <stdio.h>
int main(void)
{
    int x;
    x = 5;
    x = x + 1;
    printf("%d", x); // output 5

    return (0);
}

Likewise, a newer and shorter way to rewrite x = x + 1 is x += 1 . Given a variable x with a value of 5, x += 1 set the new value of x to be equal to 5 + 1. Hence, the value becomes 6.

#include <stdio.h>
int main(void)
{
    int x;
    x = 5;
    x += 1;
    printf("%d", x); // output 5
    return (0);
}

INCREMENT AND DECREMENT OPERATOR

The more recent method of increment is by using the operators:

  • ++ : Increment Operator

  • -- : Decrement Operator

The increment operator, represented by two consecutive plus signs ++, adds 1 to the value of an operand or variable. The decrement operator, represented by two consecutive minus signs --, subtracts 1 from the value of an operand or variable.

However, things can get confusing when you realize that there are two methods to execute an increment and decrement operation, respectively. These methods are:

  • Pre-Increment Operation

  • Post-Increment Operation

  • Post-decrement Operation

  • Pre-decrement Operation

PRE-INCREMENT OPERATOR

The pre-increment operator has the operator placed before the variable. Such as ++x

#include <stdio.h>
int main(void)
{
    Int x, y;
    x = 5
    y = ++x;
    printf("%d", x); //output 6
    printf("%d", y); //output 6

    return (0);
}

After execution, the value of x and y were incremented to 6 as expected.

Here are the implicit workings of the operation.

y = ++x;
x = x + 1; // first step
y = x;     // second step

//new value of x is 6
//new value of y is 6

The code above shows how the expression is executed.

  1. The post-increment operator ++ has higher precedence than the assignment operator = . Therefore the value of x is incremented.

  2. The new value of x is assigned to y.

POST-INCREMENT OPERATION

An Increment can be post-increment if the operator is placed after the variable. Such as x++ .

#include <stdio.h>
int main()
{
    int x, y;
    x = 5;
    y = x++;
    printf("%d", x); // x = 6
    printf("%d", y); // y = 5
}

In the code above, we declared two variables x and y and assigned a value of 5 to x. In the third statement, the value of y is assigned to be the post-increment value of y. However, neither the value of x nor y was incremented when the results were printed to standard output. This behaviour can be explained by the fact that the post-increment operator only increments the value of y after it has been used in the assignment statement.

Here are the implicit workings involved in post-increment operation

y = x++;
y = x;     // 1. first step
x = x + 1; // 2. second step

//new value of x is 6
//new value of y is 5 (no changes)
  1. First, the value of x is assigned to y.

  2. Second, x is incremented.

This was the bug I encountered. Although I knew about these operators, I didn't learn there was a deviation in their operation.

The post-increment/decrement operator is useful in procedures where there's a need to evaluate a variable's value after using it in an expression. It allows you to perform calculations or assignments using the original value and then update the variable's value for subsequent use.

The code below is a program that converts a decimal to hexadecimal.

#include <stdio.h>
void decToHex(int decimal, char hex[])
{
    int quotient = decimal;
    int remainder;
    int index = 0;

    while (quotient != 0)
    {
        remainder = quotient % 16;
        if (remainder < 10)
            hex[index++] = remainder + '0'; // Convert remainder to character
        else
            hex[index++] = remainder - 10 + 'A'; // Convert remainder to hexadecimal(A-F)
        quotient = quotient / 16;
    }
    // Reverse the hexadecimal string
    int i;
    int length = index;
    for (i = 0; i < length / 2; i++)
    {
        char temp = hex[i];
        hex[i] = hex[length - 1 - i];
        hex[length - 1 - i] = temp;
    }
    hex[length] = '\0'; // Add null-terminating character
}

int main()
{
    int decimal = 27;
    char hex[18];
    decToHex(decimal, hex);
    printf("Hexadecimal: %s\n", hex); // output: 1B
    return 0;
}

index++ is a post-increment operator, which means that the value of index is incremented after the current expression has been evaluated. In this case, the current expression is remainder + '0' or remainder - 10 + 'A', depending on the value of remainder. Therefore, the first value of index that is used in the expression is 0. After that, index is incremented by 1.

Initially, I tried to convert the remainder to a character using the ++index operator, but I did not get the desired result. After carefully studying the operators' workings, I realized my mistake and had to fix it.

POST-DECREMENT OPERATION

The post-decrement operation is represented by the -- operator placed on the right side of an operand or variable. Similar to the post-increment operation, the value is decremented after the current expression has been evaluated. This means that the original value of the operand or variable is used in the expression, and then decremented by 1.

#include <stdio.h>
int main(void)
{
    int x, y;
    x = 5;
    y = x--;  //post-decrement
    printf("%d", x); //output 4
    printf("%d", y); // output 5
}

When the statement y = x-- is executed, the assignment of y to the value of x happens before the decrement of x. In other words, the current value of x is assigned to y, and then x is decremented by 1.

PRE-DECREMENT OPERATION

The pre-decrement operation has the operation placed before the operand or variable --x .

#include <stdio.h>
int main()
{
    int x, y;
    x = 5;
    y = --x;  //pre-decrement
    printf("%d", x); //output 4
    printf("%d", y); // output 4
}

In the code, above the operator sign was placed before the operand. After execution, the value of x and y were decremented to 4 as expected.

Here are the implicit workings of the operation.

y = --x;
x = x - 1;  // 1. first step
y = x;     // 2. second step

//new value of x is 4
//new value of y is 4
  1. The operator is executed on x

  2. The new value of x is assigned to y

Further Examples

#include <stdio.h>
int main()
{
    int a, b, x, y;
    a = 22;
    b = 23;

    x = 9;
    y = 10;
    while (++x < y)    // line x
        printf("value of x becomes: %d\n ", x); // no output
    while (a++ < b)    // line a
         printf("value of a becomes: %d ", a);  //output = 23
    return (0);
}

The while statement in line "x" has a condition that specifies printing the value of x while the pre-increment of x is less than y. In this case, the post-increment of x is 23, which falsifies the condition, and so no value is printed to the standard output.

The line "a" contains a while statement with a condition that specifies printing the value of a while the pre-increment of a is less than b. In this case, the pre-increment of a is 23, which satisfies the condition, and so the value is printed to the standard output.

Summary

  1. The statement y = ++x . The value of x is assigned to y after x is incremented. Likewise --x.

  2. The statement y = x++. The assignment of x to y takes place first, and then x is incremented. Likewise x--

  3. The pre-increment/decrement operator has higher precedence than the assignment operator. Therefore ++/-- is evaluated first.