Operators
Operators are symbols used to manipulate data.
In the expression z = x + y
, z
, x
, and y
are operands, while =
and +
are operators.
Operators Based on the Number of Operands
-
Unary
- 1 operand
- Examples:
x++
,++x
,+x
,-x
-
Binary
- 2 operands
- Examples:
x + y
,x - y
,x! = y
,x==y
-
Ternary
- 3 operands
- Only one operator:
?:
Operators on basis of functions
- Arithmetic
- Assignment
- Relational
- Logical
- Increment
- Decrement
- Shorthand
- Ternary
- Size of operator
- Miscellaneous
General order of Precedence from high to low:
- Postfix operators (all have the same precedence, so sequences of operators will be evaluated left-to-right)
- array subscript operator
[]
- function call operator
()
- component selection operators
.
and->
- postfix
++
and--
- array subscript operator
- Unary operators (all have the same precedence, so sequences of operators will be evaluated left-to-right)
- prefix
++
and--
sizeof
- bitwise negation operator
~
- logical negation operator
!
- unary sign operators
-
and+
- address-of operator
&
- dereference operator
*
- prefix
- Cast expressions
(
type name)
- Multiplicative operators
*
,/
,%
- Additive operators
+
and-
- Shift operators
<<
and>>
- Relational operators
<
,>
,<=
,>=
- Equality operators
==
and!=
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Logical AND
&&
- Logical OR
||
- Conditional operator
?:
- Assignment operators
=
,+=
.-=
,*=
,/=
,%=
,<<=
,>>=
,&=
,^=
,|=
- Sequential (comma) operator
,
So, expressions like*x++
are parsed as*(x++)
, since the postfix++
has higher precedence than the unary*
.
Similarly,sizeof x + 1
is parsed as(sizeof x) + 1
, sincesizeof
has higher precedence than addition.
An expression likep++->x
is parsed as(p++)->x
; both postfix++
and->
operators have the same precedence, so they’re
parsed from left to right.
When in doubt, use parentheses.
Associativity
Associativity refers to the order in which operators of the same precedence are evaluated in an expression.
There are two types of associativity: left associativity and right associativity.
-
Left Associativity:
- For operators with left associativity, evaluation proceeds from left to right. This means that if multiple operators of the same precedence appear consecutively in an expression, they are evaluated from left to right.
int result = 10 - 5 + 2;
In this expression, both the subtraction (
-
) and addition (+
) operators have the same precedence. Since they are left-associative, the subtraction operation10 - 5
is evaluated first, followed by the addition operation5 + 2
. -
Right Associativity:
- Conversely, for operators with right associativity, evaluation occurs from right to left. This means that if multiple operators of the same precedence appear consecutively, they are evaluated from right to left.
`int result = 2 ^ 3 ^ 2;`
In this expression, the bitwise XOR operator (
^
) has right associativity. Therefore, the evaluation starts from the rightmost operator3 ^ 2
, and then the result is XORed with2
.
Understanding associativity is crucial for correctly interpreting expressions and determining the order of operations. It helps in writing code that produces the expected results and avoids ambiguity in complex expressions.