Increment and Decrement Operators in C

It is used to increment or decrement the original value of a variable by 1. It is a unary operator. This operator can either be prepended or postpended. And the nature of the operation is different based on the position of the operator.

Postfix: The expression is evaluated first using the original value of the variable and then the variable which has an operator is incremented or decremented by one. For example, let x = 10;y = x++; then after the operation, the value of y is 10 and that of x is 11.

Prefix: The variable that has the operator is first incremented/decremented, and then the expression is evaluated using the new value of the variable. For example, let x = 10; y = ++x; then after the operation, the value of x is 11 and that of y is also 11.

OperatorDescription
++xpre-increment
x++post-increment
–xpre-decrement
x–post-decrement
Increment/Decrement Operators

Example:

// increment decrement operators
#include <stdio.h>

int main(){
    int w = 4, x = 10, y = 15, z = 2, a = 0, b = 0, c = 0, d = 0;

    printf("Before Operation(a = x++) a = %d && x = %d\n", a, x);
    a = x++;
    printf("After Operation(a = x++) a = %d && x = %d\n", a, x);

    printf("Before Operation(b = ++y) b = %d && y = %d\n", b, y);
    b = ++y;
    printf("After Operation(c = ++y) b = %d && y = %d\n", b, y);

    printf("Before Operation(c = z--) c = %d && z = %d\n", c, z);
    c = z--;
    printf("After Operation(c = z--) c = %d && z = %d\n", c, z);

    printf("Before Operation(d = --w) d = %d && w = %d\n", d, w);
    d = --w;
    printf("After Operation(d = --w) d = %d && w = %d\n", d, w);

    return 0;
}

Sample Output:

Before Operation(a = x++) a = 0 && x = 10
After Operation(a = x++) a = 10 && x = 11
Before Operation(b = ++y) b = 0 && y = 15
After Operation(c = ++y) b = 16 && y = 16
Before Operation(c = z--) c = 0 && z = 2
After Operation(c = z--) c = 2 && z = 1
Before Operation(d = --w) d = 0 && w = 4
After Operation(d = --w) d = 3 && w = 3