C Operators
The C operators help you to manipulate operands and build expressions. The C operators are used for assignment expressions, arithmetic expressions, logical expressions, relational expressions and many more.
What is Operand and Operator
Expressions are made up of operators and operands. For instance, the expression 4 + 5 resolves to 9. In this case 4 and 5 are operands and + is an operator. The operands can be of any of the C data types, objects or arrays. It could be a constant, a variable or a function result.
What is Unary Operator and Binary Operator
The operators which perform operation using two operands are called binary operators, on the other hand operators which need one operand are called unary operators. The C language also supports one ternary operator, the conditional operator ?:. The ternary operator uses three expressions to produce a single expression.
Operator Precedence
In general, expressions are evaluated from left to right. But some operators have higher precedence over the other operators. The precedence determines the order in which expressions are evaluated.
For instance, in the expression 3 + 4 * 3, the result is 15 and not 21 because the multiplication operator * has a higher precedence than the addition operator +. Multiplication is performed before addition.
For more details about C operators and their precedence order visit list of C operators.
Operator Associativity (Order of Evaluation)
The associativity of an operator is also important, as it determines whether operations are performed from left to right or right to left. The associativity of an operator also affects the order in which operations of the same precedence are performed.
For instance - is left-to-right associative, so 2 - 3 - 4 is grouped as (2 - 3) - 4 and evaluates to -5. On the other hand = is right-to-left associative, so i = j = k is grouped as i = (j = k).
For further details about operators and their associativity, please visit list of C operators.
Operator Side Effects
Some expressions never affect the state of the program. On the other hand, some expressions have side effects. For instance,
- The expression 3 + 4 resolves to 7. It has no effect on the state of the program.
- The assignment operator = is one of the examples, which has side effects. For instance, the expression x = 4, it assigns the value 4 to variable x and it changes the value of the expression that uses the variable x.
There are few more examples of such expressions, we will discuss in next sections.