In the C programming language, there are various groups of operators that can be used to operate on data. In this blog post, we will explore these operators, specifically focusing on arithmetic, comparison, logical, compound assignment, and miscellaneous operators. We will use two imaginary variables, a
and b
, as examples throughout the post.
Arithmetic Operators
Arithmetic operators can be classified into two types: binary and unary operators. Binary operators work on two operands, while unary operators work on a single operand.
Binary operators:
Operator | Name | Example |
---|---|---|
= |
Assignment | a = b |
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulo | a % b |
Unary operators:
Operator | Name | Example |
---|---|---|
+ |
Unary plus | +a |
- |
Unary minus | -a |
++ |
Increment | a++ or ++a |
-- |
Decrement | a-- or --a |
The difference between a++
and ++a
lies in the order of incrementing the variable a
. a++
increments a
after using its current value, while ++a
increments a
before using its incremented value.
For example:
int a = 2;
int b;
b = a++; /* b is 2, a is 3 */
b = ++a; /* b is 4, a is 4 */
The same applies to the decrement operator.
Comparison Operators
Comparison operators are used to compare two values and result in a boolean value.
Operator | Name | Example |
---|---|---|
== |
Equal | a == b |
!= |
Not equal | a != b |
> |
Bigger than | a > b |
< |
Less than | a < b |
>= |
Bigger than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Logical Operators
Logical operators are used to combine boolean values and produce a boolean result.
!
(NOT): Example:!a
&&
(AND): Example:a && b
||
(OR): Example:a || b
These operators are particularly useful when working with boolean values.
Compound Assignment Operators
Compound assignment operators are shortcuts for performing an assignment and an arithmetic operation simultaneously.
Operator | Name | Example |
---|---|---|
+= |
Addition | a += b |
-= |
Subtraction | a -= b |
*= |
Multiplication | a *= b |
/= |
Division | a /= b |
%= |
Modulo | a %= b |
Miscellaneous Operators
The Ternary Operator
The ternary operator is unique to C and is used to express conditionals in a concise way. It takes three operands and has the following syntax:
<condition> ? <expression> : <expression>
For example:
a ? b : c
If a
is evaluated as true, the first expression (b
) is executed; otherwise, the second expression (c
) is executed. The ternary operator can be considered as a shorter alternative to an if-else conditional statement and can be inline within an expression.
sizeof Operator
The sizeof
operator is used to determine the size, in bytes, of the operand. The operand can be a variable or a data type.
Example usage:
#include <stdio.h>
int main(void) {
int age = 37;
printf("%ld\n", sizeof(age));
printf("%ld", sizeof(int));
}
Operator Precedence
When using multiple operators in a single expression, we need to consider their precedence. The C language has a set of rules to determine the order in which the operators are evaluated. The table below shows the precedence of the operators covered in this post:
=
Assignment+
and-
(binary operators)*
and/
+
and-
(unary operators)
Operators are evaluated in a left-to-right manner, except for unary operators and assignment operators.
For example, in the expression:
int a = 2;
int b = 4;
int c = b + a * a / b - a;
The value of c
can be determined by applying the operator precedence rules: a * a / b
evaluates to 4
, and then, b + 4 - 2
results in 3
.
It’s worth noting that parentheses can be used to override the operator precedence and make the expression easier to read and understand. Parentheses have the highest priority in evaluating an expression.
The same expression can be rewritten using parentheses:
int c = b + ((a * a) / b) - a;
By using parentheses, it becomes clear that the multiplication operation needs to be performed first.
Tags: C Operators, Operator Precedence, Arithmetic Operators, Comparison Operators, Logical Operators, Compound Assignment Operators, Ternary Operator, sizeof
Operator