Ads 468x60px

Labels

Thursday, 6 September 2012

Operators in C


There are three types of operators. A unary expression consists of either a unary operator prepended to an operand, or the sizeof keyword followed by an expression. The expression can be either the name of a variable or a cast expression. If the expression is a cast expression, it must be enclosed in parentheses. A binary expression consists of two operands joined by a binary operator. A ternary expression consists of three operands joined by the conditional-expression operator.

C includes the following unary operators:

SymbolName
– ~ !
Negation and complement operators
* &
Indirection and address-of operators
sizeof
Size operator
+
Unary plus operator
++ ––
Unary increment and decrement operators

Binary operators associate from left to right. C provides the following binary operators:
SymbolName
* / %
Multiplicative operators
+ –
Additive operators
<<   >>
Shift operators
<   >   <=   >=   ==   !=
Relational operators
&   | ^
Bitwise operators
&&   ||
Logical operators
,
Sequential-evaluation operator

C has one ternary operator: the conditional-expression operator (? :).      

conditional-expression:
logical-OR-expression
logical-OR expression expression conditional-expression

The following examples show uses of the conditional operator:
j = ( i < 0 ) ? ( -i ) : ( i );