Saturday 7 January 2012

Unary Operators

‘C’ includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators.
Unary operators usually precedes their single operands, though some unary operators are written after their operands.

The most common unary operator is unary minus, where a minus sign precedes a numerical constant, a variable or an expression.
e.g.
-5,-10, -20(numbers)
x=-y(variable)

Of all the arithmetic operators, the unary minus has the highest precedence level. Thus in an expression such as
y=x+z* -b;
evaluation commences with the unary minus, which negates the value of b. Then z is multiplied by –b, and finally the addition takes place.

Two other commonly used unary operators are increment operator, ++, & the decrement operator - -. 
The increment operator causes its operand to increased by one, whereas the decrement operator causes
its operand to be decreased by one. 
The operand used with each of these operators must be a single variable.
For example, x is an integer variable that has been assigned a value of 10. The expression ++ x, which is equivalent to writing x= x+1, causes the value of x to be creased to 11. 
Similarly the expression --x, which is equivalent to x=x-1, causes the original value of x to be decreased to 9.

The increment and decrement operators can each be utilized in two different ways, depending on whether the operator is written before or after the operand.
Another unary operator is the sizeof operator . This operator returns the size of its operand, in bytes. This operator always precedes its operand. The operand may be an expression, or it may be a cast.
e.g 
sizeof (x);
sizeof (y);

If x is of integer type variable and y is of floating point variable then
the result in bytes is 2 for integer type and 4 for floating point
variable.

No comments:

Post a Comment

host gator coupon