1) Increment/Decrement | Unary Operators:
Increment/decrement operator is also known as unary operator. Unary operators, meaning it operate on a singal operand. The increment operator (++) is used to increase by 1 to its operand and the decrement operator decrease by one 1 to its operand.
For example: X++ (also can be written X = X + 1) and X-- (or X = X - 1) where, X is a variable which is called operand. And ++ and -- are increment and decrement operators.
Increment/decrement operators are of two types prefix and postfix. Let's suppose x = 10 . |
++ | Increment operator will add 1 to its operand. | x++ will result to 11
++x will result to 11 |
-- | Decrement operator will subtract 1 from an integer value. | x-- will result to 9
--x will result to 9 |
Important note: ++
and --
used with variables (++
and --
operators need only one operand to perform an operation). Using ++
or --
with constant will result in error. Such as expressions like 10++
(x+y)++
etc. are invalid.
Stntax for increment/decrement operator
Increment/decrement operators are of two types prefix and postfix.
Syntax | Description | Example |
---|
++<variable-name> | Pre increment | ++x |
<variable-name>++ | Post increment | x++ |
--<variable-name> | Pre decrement | --x |
<variable-name>-- | Post decrement | x-- |
Let's consider an integer variable int x = 10
. To increment x
by 1, you can use either
x = x + 1
(Simple assignment)
x += 1
(Shorthand assignment)
x++
(Post increment)
++x
(Pre increment)
Result of all the above code are same.
Example of ++
increment operator:
Both post increment x++
(i.e x = x + 1) / pre increment ++x
(i.e x = x + 1) does the same task of incrementing the value by 1 to its operand.
#include <stdio.h>
int main()
{
int X=3;
X++;
printf("X=%d, X);
++X;
printf("X=%d, X);
return 0;
}
Output of above program is x = 4
x = 5
. Let us understand the code.
x++
Since we have used postfix notation (i.e x = x + 1). Hence, It will increments the current value of x
by 1 to 4.
++x
Here we have used prefix notation (i.e x = x + 1). Hence, It will also increments the current value of x
by 1 to 5.
Example of -- decrement operator:
Both post decrement x--
(i.e x = x-1) / pre-decrement --x
w (i.e x = x-1) does the same task of decrementing the value by 1 to its operand.
#include <stdio.h>
int main()
{
int X=3;
X--;
printf("X=%d, X);
--X;
printf("X=%d, X);
return 0;
}
Output of above program is x = 2
x = 1
.
Difference between prefix and postfix in C:
#include <stdio.h>
int main()
{
int a, b, c;
a = 10;
b = ++a;
c = a++;
printf("a=%d, b=%d, c=%d", a, b, c);
return 0;
}
Output of above program is a=12, b=11, c=11. Let us understand the code.
a = 10
Simply assigns 10 to variable a
b = ++a
Since we have used prefix notation. Hence, first it increments the value of a to 11, then assigns the incremented value of a to b.
c = a++
Here we have used postfix notation. Hence, first it assigns the current value of a i.e. 11 to c, then increments the value of a to 12.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.