The increment operator (++) increments the value of its operand by 1.

The decrement operator(--) decrements the value of its operand by 1;

Both the operators can be placed before or after the operand as shown above and they can be applied on primitive data types like char, int, float, double & they can also be used on pointers and enums.

Value of

Value of

In the above expression, the value of

Value of

Value of

Here, the value of

Value of

Value of

In the above expression, the value of

Value of

Value of

Here, the value of

*x = 10;***// increments the value of x by 1. So, the value of x is 11.***x++;*The decrement operator(--) decrements the value of its operand by 1;

*x = 10;***// decrements the value of x by 1; So, the value of x is 9.***x--;*Both the operators can be placed before or after the operand as shown above and they can be applied on primitive data types like char, int, float, double & they can also be used on pointers and enums.

**Difference between postfix increment and prefix increment:****Example 1:***int a, b = 10;**a = b++;*Value of

**is 10***a*Value of

**is 11***b*In the above expression, the value of

**is assigned to***b***first. Then,***a***is added to the value of***1***.***b***Example 2:***int a, b = 10**a = ++b;*Value of

**is 11***a*Value of

**is 11***b*Here, the value of

**is incremented(add 1 to b) first. Then, the incremented value of***b***is assigned to***b***.***a***Difference between prefix decrement and postfix decrement****Example 1:***int a, b = 10;**a = b--;*Value of

**is 10***a*Value of

**is 9***b*In the above expression, the value of

**is assigned to***b***first. Then,***a***is subtracted from the value of***1***.***b***Example 2:***int a, b = 10**a = --b;*Value of

**is 9***a*Value of

**is 9***b*Here, the value of

**is decremented(subtract***b***from***1***) first. Then, the decremented value of***b***is assigned to***b***.***a***Example C program on increment and decrement operators:**

#include <stdio.h> int main() { int x = 10, y, num; enum fruit {APPLE, ORANGE, BANANA, JACK}; enum fruit f1 = APPLE; /* * incrementing the value of variable of type enum * and assigning the same to interger variable num */ num = ++f1; printf("value of num is %d\n", num); printf("value of f1 is %d\n\n", f1); /* printing the original value of x */ printf("Value of x before incrementing: %d\n", x); /* incrementing the value of x and printing its value */ x++; printf("Value of x after incrementing: %d\n\n", x); /* printing the value of x before dementing */ printf("Value of x before decrementing: %d\n", x); /* decrementing the value of x and priting its value */ x--; printf("Value of x after decrementing: %d\n\n", x); printf("Tricks with increment/decrement operators\n"); /* * assigns value of x to y first and * then increments the value of x */ y = x++; printf("Value of x:%d\nValue of y: %d\n\n", x, y); y = 0; /* * increments x first and * then assigns the value of x to y */ y = ++x; printf("Value of x: %d\nValue of y: %d\n\n", x, y); y = 0; /* * assigns the value of x to y first and * then decrements the value of x */ y = x--; printf("Value of x:%d\nValue of y: %d\n\n", x, y); y = 0; /* * decrements the value of x first and * then assigns the value of x to y */ y = --x; printf("Value of x: %d\nValue of y: %d\n\n", x, y); return 0; }

**Output:**

jp@jp-VirtualBox:~/cpgms/posts$ ./a.out

value of num is 1

value of f1 is 1

Value of x before incrementing: 10

Value of x after incrementing: 11

Value of x before decrementing: 11

Value of x after decrementing: 10

Tricks with increment/decrement operators

Value of x:11

Value of y: 10

Value of x: 12

Value of y: 12

Value of x:11

Value of y: 12

Value of x: 10

Value of y: 10

value of num is 1

value of f1 is 1

Value of x before incrementing: 10

Value of x after incrementing: 11

Value of x before decrementing: 11

Value of x after decrementing: 10

Tricks with increment/decrement operators

Value of x:11

Value of y: 10

Value of x: 12

Value of y: 12

Value of x:11

Value of y: 12

Value of x: 10

Value of y: 10

## No comments:

## Post a Comment