Increment and Decrement Operators in C

C - Increment and Decrement Operators

The increment operator (++) increments the value of a variable by 1, while the decrement operator (—) decrements the value.

Increment and decrement operators are frequently used in the construction of counted loops in C (with the for loop). They also have their application in the traversal of array and pointer arithmetic.

The ++ and — operators are unary and can be used as a prefix or posfix to a variable.

Example of Increment and Decrement Operators

The following example contains multiple statements demonstrating the use of increment and decrement operators with different variations −

#include <stdio.h>

int main() {
  int a = 5, b = 5, c = 5, d = 5;
  
  a++; // postfix increment
  ++b; // prefix increment
  c--; // postfix decrement
  --d; // prefix decrement

  printf("a = %d\n", a);
  printf("b = %d\n", b);
  printf("c = %d\n", c);
  printf("d = %d\n", d);

  return 0;
}

Output

When you run this code, it will produce the following output −

a = 6
b = 6
c = 4
d = 4

Example Explanation

In other words, “a++” has the same effect as “++a”, as both the expressions increment the value of variable “a” by 1. Similarly, “a—” has the same effect as “—a”.

The expression “a++;” can be treated as the equivalent of the statement “a = a + 1;”. Here, the expression on the right adds 1 to “a” and the result is assigned back to 1, therby the value of “a” is incremented by 1.

Similarly, “b—;” is equivalent to “b = b – 1;”.

Types of Increment Operator

There are two types of increment operators – pre increment and post increment.

Pre (Prefix) Increment Operator

In an expression, the pre-increment operator increases the value of a variable by 1 before the use of the value of the variable.

Syntax

++variable_name;

Example

In the following example, we are using a pre-increment operator, where the value of “x” will be increased by 1, and then the increased value will be used in the expression.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + ++x;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

When you run this code, it will produce the following output −

x = 11, y = 21

Post (Postfix) Increment Operator

In an expression, the post-increment operator increases the value of a variable by 1 after the use of the value of the variable.

Syntax

variable_name++;

Example

In the following example, we are using post-increment operator, where the value of “x” will be used in the expression and then it will be increased by 1.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + x++;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

When you run this code, it will produce the following output −

x = 11, y = 20

Types of Decrement Operator

There are two types of decrement operators – pre decrement and post decrement.

Pre (Prefix) decrement Operator

In an expression, the pre-decrement operator decreases the value of a variable by 1 before the use of the value of the variable.

Syntax

--variable_name;

Example

In the following example, we are using a pre-decrement operator, where the value of “x” will be decreased by 1, and then the decreased value will be used in the expression.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + --x;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

When you run this code, it will produce the following output −

x = 9, y = 19

Post (Postfix) Decrement Operator

In an expression, the post-decrement operator decreases the value of a variable by 1 after the use of the value of the variable.

Syntax

variable_name--;

Example

In the following example, we are using post-decrement operator, where the value of “x” will be used in the expression and then it will be decreased by 1.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + x--;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

When you run this code, it will produce the following output −

x = 9, y = 20

More Examples of Increment and Decrement Operators

Example 1

The following example highlights the use of prefix/postfix increment/decrement −

#include <stdio.h>

int main(){

   char a = 'a', b = 'M';
   int x = 5, y = 23;

   printf("a: %c b: %c\n", a, b);
   
   a++;
   printf("postfix increment a: %c\n", a);
   
   ++b;
   printf("prefix increment b: %c\n", b);

   printf("x: %d y: %d\n", x, y);
   
   x--;
   printf("postfix decrement x : %d\n", x);
   
   --y;
   printf("prefix decrement y : %d\n", y);

   return 0;
}

Output

When you run this code, it will produce the following output −

a: a b: M
postfix increment a: b
prefix increment b: N

x: 5 y: 23
postfix decrement x: 4
prefix decrement y: 22

The above example shows that the prefix as well as postfix operators have the same effect on the value of the operand variable. However, when these ”++” or ”—” operators appear along with the other operators in an expression, they behave differently.

Example 2

In the following code, the initial values of “a” and “b” variables are same, but the printf() function displays different values −

#include <stdio.h>

int main(){

   int x = 5, y = 5;

   printf("x: %d y: %d\n", x,y);
   printf("postfix increment x: %d\n", x++);
   printf("prefix increment y: %d\n", ++y);

   return 0;
}

Output

Run the code and check its output −

x: 5 y: 5
postfix increment x: 5
prefix increment y: 6

In the first case, the printf() function prints the value of “x” and then increments its value. In the second case, the increment operator is executed first, the printf() function uses the incremented value for printing.

Operator Precedence of Increment and Decrement Operators

There are a number of operators in C. When multiple operators are used in an expression, they are executed as per their order of precedence. Increment and decrement operators behave differently when used along with other operators.

When an expression consists of increment or decrement operators alongside other operators, the increment and decrement operations are performed first. Postfix increment and decrement operators have higher precedence than prefix increment and decrement operators.

Example 1

Take a look at the following example −

#include <stdio.h>

int main(){

   int x = 5, z;

   printf("x: %d \n", x);

   z = x++;
   printf("x: %d z: %d\n", x, z);

   return 0;
}

Output

Run the code and check its output −

x: 5 
x: 6 z: 5

Since “x++” increments the value of “x” to 6, you would expect “z” to be 6 as well. However, the result shows “z” as 5. This is because the assignment operator has a higher precedence over postfix increment operator. Hence, the existing value of “x” is assigned to “z”, before incrementing “x”.

Example 2

Take a look at another example below −

#include <stdio.h>

int main(){

   int x = 5, y = 5, z;

   printf("x: %d y: %d\n", x,y);

   z = ++y;
   printf("y: %d z: %d\n", y ,z);

   return 0;
}

Output

When you run this code, it will produce the following output −

y: 5
y: 6 z: 6

The result may be confusing, as the value of “y” as well as “z” is now 6. The reason is that the prefix increment operator has a higher precedence than the assignment operator. Hence, “y” is incremented first and then its new value is assigned to “z”.

The associativity of operators also plays an important part. For increment and decrement operators, the associativity is from left to right. Hence, if there are multiple increment or decrement operators in a single expression, the leftmost operator will be executed first, moving rightward.

Example 3

In this example, the assignment expression contains both the prefix as well as postfix operators.

#include <stdio.h>

int main(){

   int x = 5, y = 5, z;

   z = x++ + ++y;
   printf("x: %d y: %d z: %d\n", x,y,z);

   return 0;
}

Output

Run the code and check its output −

x: 6 y:6 z: 11

In this example, the first operation to be done is “y++” (“y” becomes 6). Secondly the ”+” operator adds “x” (which is 5) and “y”, the result assigned to “z” as 11, and then “x++” increments “x” to 6.

Using the Increment Operator in Loop

In C, the most commonly used syntax of a for loop is as follows −

for (init_val; final_val; increment) {
   statement(s);
}

Example

The looping body is executed for all the values of a variable between the initial and the final values, incrementing it after each round.

#include <stdio.h>

int main(){

   int x;

   for (x = 1; x <= 5; x++){
      printf("x: %d\n", x);
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −

x: 1
x: 2
x: 3
x: 4
x: 5
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team