Increment and Decrement Operator in C
Increment and decrement operators in C
The increment operator (++) increments the value of its operand by 1.x = 10;
x++; // increments the value of x by 1. So, the value of x is 11.
The decrement operator(--) decrements the value of its operand by 1;
x = 10;
x--; // decrements the value of x by 1; So, the value of x is 9.
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 a is 10
Value of b is 11
In the above expression, the value of b is assigned to a first. Then, 1 is added to the value of b.
Example 2:
int a, b = 10
a = ++b;
Value of a is 11
Value of b is 11
Here, the value of b is incremented(add 1 to b) first. Then, the incremented value of b is assigned to a.
Difference between prefix decrement and postfix decrement
Example 1:
int a, b = 10;
a = b--;
Value of a is 10
Value of b is 9
In the above expression, the value of b is assigned to a first. Then, 1 is subtracted from the value of b.
Example 2:
int a, b = 10
a = --b;
Value of a is 9
Value of b is 9
Here, the value of b is decremented(subtract 1 from b) first. Then, the decremented value of b is assigned to a.
All In One EXAMPLE:-
OUTPUT
Thanks For Visiting Visit Again...🌝🌝🌝🌝🌝
Comments
Post a Comment