Increment and Decrement Operators
The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below
1. ++ variable name
2. variable name++
3. – –variable name
4. variable name– –
The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of the operand. ++variable name and variable name++ mean the same thing when they form statements independently, they behave differently when they are used in the expression on the right-hand side of an assignment statement.
Consider the following .
m = 5;
y = ++m; (prefix)
In this case, the value of y and m would be 6
Suppose if we rewrite the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.
Post A Comment:
0 comments: