C Increment Operator

C Increment Operator increments the given value by one.

Increment operator takes only one operand. There are two forms for Increment Operator based on the side of operator at which the operand is given, left of right. They are:

  • Prefix Increment
  • Postfix Increment

In terms of execution, the main difference between prefix and postfix increments is that: Prefix increments the variable value and then participates in the expression evaluation or statement execution. But postfix first lets the expression evaluation or statement execution complete and then increments the value of the operand.

Prefix Increment

For Prefix Increment, the operand comes to the right of the operator. The syntax is given below.

++operand

Example

In the following example, we take an integer and increment it using prefix form.

main.c

#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Increment : %d\n", a);
    
    ++a;
    printf("After  Increment : %d\n", a);
}

Output

Before Increment : 4
After  Increment : 5
Program ended with exit code: 0
ADVERTISEMENT

Postfix Increment

For Postfix Increment, the operand comes to the left of the operator.

operand++

Example

In the following example, we take an integer and increment it using postfix form.

main.c

#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Increment : %d\n", a);
    
    a++;
    printf("After  Increment : %d\n", a);
}

Output

Before Increment : 4
After  Increment : 5
Program ended with exit code: 0

Postfix vs Prefix Increment

Let us check the difference of postfix and prefix using an example program.

Example

In the following example, we shall perform postfix and prefix on different variables, and then look into how they act during execution.

main.c

#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Post-Increment : %d\n", a); //4
    printf("During Post-Increment : %d\n", a++); //4
    printf("After  Post-Increment : %d\n\n", a); //5
    
    int b = 8;
    printf("Before Post-Increment : %d\n", b); //4
    printf("During Pre-Increment : %d\n", ++b); //9
    printf("After  Pre-Increment : %d\n", b); //9

Output

Before Post-Increment : 4
During Post-Increment : 4
After  Post-Increment : 5

Before Pre-Increment : 8
During Pre-Increment : 9
After  Pre-Increment : 9
Program ended with exit code: 0

Increment Float Value

Increment operator can take a float variable as operand and increase its values by 1.

Example

In the following example, we shall initialize a float variable with some value and increment its value by 1 using ++ operator.

main.c

#include <stdio.h>

int main() {
    float a = 4.2;
    printf("Before Increment : %f\n", a);
    
    ++a;
    printf("After  Increment : %f\n", a);
}

Output

Before Increment : 4.200000
After  Increment : 5.200000
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned what increment operator does, how to use increment operator in prefix or postfix form, with the help of examples.