C has two useful operators : (1) Increment Operator ( ++ ) (2) Decrement Operator (-- ) . These operators are unary operators. They operate on single operand. The operator ++ adds 1 to its operand and -- subtracts 1. Both the operators should not be used with constants. Both the increment and decrement operators may either prefix or postfix. In other words we can say that both the increment and decrement operators may either precede or follow the operand.
Prefix Increment (++a ) / Decrement (--a ) :
main( )
{
..........
x = 5;
y = -- x;
..........
}
this sets y = 4.
When an increment or decrement operator precedes its operand, the incement or decrement operation is performed before obtaining the value of operand for use in the expression.
Postfix Increment ( a++ ) / Decrement ( -- ) :
main( )
{
..........
x = 5;
y = x++;
z = x;
..........
}
this sets y = 5 and z = 6.
When an increment or decrement operator follows its operand, the increment or decrement operation is performed after obtaining the value of operand for use in the expression.
0 Comments