Increment and Decrement Operators in C

The Increment and Decrement Operators in C are some of the operators that are used to increase or decrease the value by 1 and it allows to prefix or postfix the operator. For instance, the Incremental operator ++ is used to increase the existing variable value by 1 (x = x + 1). And decrement operator – – is used to decrease or subtract the existing value by 1 (x = x – 1).

The syntax of the increment and decrement operators in C Programming to prefix and postfix is:

  • Increment Operator: ++x (prefix) or x++ (postfix).
  • Decrement Operator: –x or x–

Increment and decrement operators in C Example

Let’s see one example of increment and decrement operators to get a better idea!

At Line 7, we used the increment operator. So the value of the X was returned first (i.e., 10), and then the X value was incremented by 1.

Line 8: We called the X value again; it displays 11 because the value has already been updated. Same with the decrement operator.

#include <stdio.h> 

int main()
{
 int x = 10,y = 20;

 printf("----INCREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of x : %d \n", x); //Original Value
 printf("Value of x : %d \n", x++); // Using increment Operator
 printf("Value of x : %d \n", x); //Incremented value
 
 printf("----DECREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of y : %d \n", y); //Original Value
 printf("Value of y : %d \n", y--); // using decrement Operator
 printf("Value of y : %d \n", y); //decremented value

 return 0;
}
Increment and decrement operators in C Example

C Prefix and Postfix

If you observe the above prefix and postfix syntax, we can assign the C increment and decrement operators either before the operand or after the operand. When ++ or — is used before an operand like ++x, –x then we call it a prefix, if ++ or — is used after the operand like: x++ or x– then we call it a postfix.

Let’s explore the prefix and postfix.

  1. ++i (Pre-increment): It will increment the value of i even before assigning it to the variable i.
  2. i++ (Post-increment): The operator will return the variable value first (i.e, i value) then only i value is incremented by 1.
  3. –i (Pre decrement): It will decrement the value of i even before assigning it to the variable i.
  4. i– (Post decrement): The operator returns the variable value first (i.e., i value), then only i value decremented by 1.

C Prefix and Postfix Example

This Program shows how to use Increment and Decrement Operators as a Prefix and Postfix.

#include<stdio.h> 

int main()
{

 int x = 10,y = 20, a = 5, b= 4;

 printf("---- PRE INCREMENT EXAMPLE---- \n");
 printf("Value of x : %d \n", x); //Original Value
 printf("Value of x : %d \n", ++x); // using Pre increment Operator
 printf("Value of x Incremented : %d \n", x); //Incremented value

 printf("----POST INCREMENT EXAMPLE---- \n");
 printf("Value of y : %d \n", y); //Original Value
 printf("Value of y : %d \n", y++); // using Post increment Operator
 printf("Value of Incremented y : %d \n", y); //Incremented value

 printf("----PRE DECREMENT EXAMPLE---- \n");
 printf("Value of a : %d \n", a); //Original Value
 printf("Value of a : %d \n", --a); // using Pre decrement Operator
 printf("Value of decremented y : %d \n", a); //decremented value

 printf("----POST DECREMENT EXAMPLE---- \n");
 printf("Value of b : %d \n", b); //Original Value
 printf("Value of b : %d \n", b--); // using Post decrement Operator
 printf("Value of decremented b : %d \n", b); //decremented value

 return 0;
 }

Output

C Programming Prefix and Postfix Example

Although increment and decrement operators are used in For Loop, While, and Do-While loops, we are not given an example. Because it is too early to understand the loop concept. Try to understand the concept of the prefix and postfix. This Operator helps you to understand if statement, For Loop, While, and Do-While loop Program syntax in C Programming easily.

Why does printf((“%d”, i++) print the old value of i instead of the incremented one?

In the printf() statement, we used the post increment (i++). As we all know, the post increment (i++) returns the current value and then increments it. To return the updated (incremented value), either use pre-increment (++i) or perform the increment operation first and then print the value.

As you see from the above example, i++ prints the old value. However, the next statement prints the incremented one.

#include <stdio.h>

int main()
{
int i = 10;
printf("%d\n", i++);
printf("%d", i);
}
10
11

C increment (++) and decrement (–) operators with pointers

When we apply the increment or decrement operators with pointers, it moves to the next address or the previous one.

In the following example, we declared a string and the pointer is set to the first character in the string (A).

  • p++ moves to the next address. So the character in the next position is B.
  • p++ prints C.
  • p– means pointer moves backwards and prints the character in the previous address, which is B.
#include <stdio.h>

int main()
{
char str[] = "ABCDEF";
char *p = str;
printf("%c\n", *p);

p++;
printf("%c\n", *p);

p++;
printf("%c\n", *p);

p--;
printf("%c\n", *p);

p--;
printf("%c\n", *p);
}
A
B
C
B
A

In the following example, we use the C increment and decrement operators to work with a pointer pointing to an array.

  • *p++ prints the first item in an array because it is a post-increment approach. So, it prints the first array element and then increments. The current pointer is pointing to the second item (20).
  • *++p (pre-increment): Pointer moves to next address and prints 30 as output.
  • *–p prints the previous array items, which are 20.
#include <stdio.h>

int main()
{
    int arr[] = {10, 20, 30};
    int *p = arr;

    printf("%d\n", *p++);
    printf("%d\n", *++p);
    printf("%d\n", *--p);
}
10
30
20

Can we use increment and decrement operators on float and double data types in C?

Yes. We can use increment and decrement operators (prefix and postfix) on float and double data types, and they work similarly to the int data type. It keeps the decimal values as they are and increments or decrements the integer part by 1.

#include <stdio.h>

int main()
{
float f = 10.5f;
double d = 100.25;

printf("%.1f\n", f++);
printf("%.1f\n", f);
printf("%.1f\n", ++f);

printf("%.1f\n", d++);
printf("%.1f\n", d);
printf("%.1f\n", ++d);
}
10.5
11.5
12.5
100.2
101.2
102.2