• C Programming Video Tutorials

Increment and Decrement Operators in C



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.

Syntax

int a = 5;
a++; // postfix increment

int b = 5;
++b; // prefix increment

int c = 5;
c--; // postfix decrement

int d = 5;
--d; // prefix decrement

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;".

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() statement 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

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

Pointer Arithmetic in C

In C language, a pointer is a variable that stores the address of another variable. The address is an integer. A pointer variable can be used as an operand with increment or decrement operator.

However, unlike the normal int, float or char variable where the increment/decrement operator changes the value by 1, in case of the pointer, its value is changed by the size of the data type.

Example

In this example, the pointer to an int variable increments by 4, as the size of int is 4. Similarly, the size of char variable is 1, hence its pointer increments by 1.

#include <stdio.h>

int main(){

   int x = 50;
   int *ptr = &x;

   printf("size of int: %d\n", sizeof(x));
   printf("The address of x is: %d\n", ptr);

   ptr++;
   printf("The address of x is: %d\n\n", ptr);

   char y = 'a';
   char *ptr1 = &y;

   printf("size of char: %d\n", sizeof(y));
   printf("The address of y is: %d\n", ptr1);

   ptr1++;
   printf("The address of y is: %d\n", ptr1);

   return 0;
}

Output

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

size of int: 4
The address of x is: 6422028
The address of x is: 6422032

size of char: 1
The address of y is: 6422027
The address of y is: 6422028
Advertisements