Operators are used to connect operands, i. e., constants and variables, to form expressions.
For example, in the expression a+s, a and s are operands and + is the operator. Depending on the number of operands on which an operator operates, the operators in C language can be grouped into three categories: unary operators, binary operators and ternary operators.
A unary operator takes only one operand, as in -x, whereas a binary operator operates on two operands, as in a+b. A ternary operator takes three operands, as in (a > b) ? a : b, where the conditional expression operator (? 🙂 is a ternary operator.
Depending on how the operators are used with the operands, there are three forms: infix operators, prefix operators and posifix operators. An infix operator is used between the operands, as in a+b and c*d. A prefix operator is used before an operand, as in -a, whereas a postfix operator is used after an operand, as in y- – , where – – is the decrement operator.
C language provides a very powerful set of operators. The commonly used operators include arithmetic, relational, equality, logical and assignment operators.
Commonly used operators in the C language
Operator Category | Description | Operators |
Arithmetic operators | Perform arithmetic operations, namely, addition (+), subtraction (- ), multiplication (*), division (/), modulo arithmetic (%), increment (++),decrement(–), negation(-) and unary plus (+) | + – * / % ++ — |
Relational operators | Perform comparison operations, namely, less than (<),greater than (>), less than or equal to (<=) and greater than or equal to (>=) | < > < = > = |
Equality operators | Perform equality (==) and inequality ( !=) tests | == != |
Logical operators | Join simple conditional expressions using AND(&&),OR (II) and NOT ( !) operations to form compound conditional expressions (Boolean expressions) | && || |
Assignment operators | Assign a value (of a constant, variable or expression) to a variable | = += -= *= /= %= |