Once you know how to store data in variables, the next natural question is how to actually do something with that data. This is exactly the role operators play in C. Operators are special symbols that instruct the compiler to perform specific operations on one or more values, whether that means adding two numbers, comparing them, or combining logical conditions to make a decision.
C offers a particularly rich set of operators compared to many other languages, partly because of its close relationship with low-level hardware operations. While this variety can feel overwhelming at first glance, most operators follow patterns that quickly become intuitive once you see them used in a few practical examples.
In this tutorial, you will learn about arithmetic operators, relational operators, logical operators, assignment operators, bitwise operators, unary operators, and the special ternary operator, along with the order in which operators are evaluated when several appear together in a single expression.
An operator is a symbol that tells the compiler to perform a mathematical, relational, or logical operation between one or more operands, which are simply the values or variables the operator acts upon. For example, in the expression a + b, the plus sign is the operator, while a and b are the operands.
#include <stdio.h>
int main() {
int mondayViews = 1500;
int tuesdayViews = 1800;
int totalViews = mondayViews + tuesdayViews;
printf("Total views: %d", totalViews);
return 0;
}
Total views: 3300
Arithmetic operators perform standard mathematical calculations and are usually the first type of operator beginners encounter, since they closely resemble the mathematics learned outside of programming.
| Operator | Description |
|---|---|
| + | Adds two operands together. |
| - | Subtracts the right operand from the left operand. |
| * | Multiplies two operands. |
| / | Divides the left operand by the right operand. |
| % | Returns the remainder after dividing the left operand by the right operand. |
#include <stdio.h>
int main() {
int totalSubscribers = 25000;
int newThisWeek = 350;
int remainder = totalSubscribers % 1000;
printf("Remainder: %d", remainder);
return 0;
}
Remainder: 0
The modulus operator, represented by the percent symbol, is especially useful for tasks such as checking whether a number is even or odd, or for extracting specific digits from a larger number.
Relational operators compare two values and produce a result indicating whether the comparison is true or false. These operators are essential whenever a program needs to make decisions based on comparing data.
| Operator | Description |
|---|---|
| == | Checks whether two operands are equal. |
| != | Checks whether two operands are not equal. |
| > | Checks whether the left operand is greater than the right operand. |
| < | Checks whether the left operand is less than the right operand. |
| >= | Checks whether the left operand is greater than or equal to the right operand. |
| <= | Checks whether the left operand is less than or equal to the right operand. |
#include <stdio.h>
int main() {
int views = 5000;
int target = 4000;
printf("Views greater than target: %d", views > target);
return 0;
}
Views greater than target: 1
In C, relational expressions evaluate to either 1, representing true, or 0, representing false, which can then be used directly in conditional statements to control program flow.
Logical operators combine multiple conditions into a single expression, allowing a program to make decisions based on more than one factor at the same time.
| Operator | Description |
|---|---|
| && | Returns true only if both conditions on either side are true. |
| || | Returns true if at least one of the conditions on either side is true. |
| ! | Reverses the result of a condition, turning true into false and false into true. |
#include <stdio.h>
int main() {
int subscribers = 25000;
int uploadsThisMonth = 6;
if (subscribers > 10000 && uploadsThisMonth >= 4) {
printf("Channel meets monetization activity requirements.");
}
return 0;
}
Channel meets monetization activity requirements.
Here, both conditions must be true simultaneously for the message to be displayed, demonstrating how logical operators allow multiple requirements to be checked together within a single condition.
Assignment operators are used to store values into variables. While the basic equals sign is the most familiar, C also provides several shorthand assignment operators that combine an arithmetic operation with assignment in a single step.
| Operator | Equivalent Expression |
|---|---|
| = | Assigns the value on the right to the variable on the left. |
| += | a += b is equivalent to a = a + b. |
| -= | a -= b is equivalent to a = a - b. |
| *= | a *= b is equivalent to a = a * b. |
| /= | a /= b is equivalent to a = a / b. |
#include <stdio.h>
int main() {
int totalViews = 4000;
totalViews += 500;
printf("Updated total views: %d", totalViews);
return 0;
}
Updated total views: 4500
Unary operators act on a single operand, unlike most other operators that require two. These are commonly used for incrementing, decrementing, or changing the sign of a value.
| Operator | Description |
|---|---|
| ++ | Increases the value of a variable by one. |
| -- | Decreases the value of a variable by one. |
| - | Reverses the sign of a value, turning positive into negative or vice versa. |
#include <stdio.h>
int main() {
int uploadCount = 9;
uploadCount++;
printf("Upload count after increment: %d", uploadCount);
return 0;
}
Upload count after increment: 10
It is worth noting that placing the increment operator before or after a variable can behave differently in more complex expressions, a distinction commonly referred to as pre-increment versus post-increment.
Bitwise operators work directly on the individual bits that make up a value's binary representation. These are less commonly used in everyday beginner programs but become important in system-level programming, embedded development, and performance-critical code.
| Operator | Description |
|---|---|
| & | Performs a bitwise AND between the corresponding bits of two values. |
| | | Performs a bitwise OR between the corresponding bits of two values. |
| ^ | Performs a bitwise XOR, setting a bit only when the corresponding bits differ. |
| ~ | Inverts all the bits of a value, commonly known as a bitwise complement. |
| << | Shifts bits to the left, effectively multiplying the value by powers of two. |
| >> | Shifts bits to the right, effectively dividing the value by powers of two. |
#include <stdio.h>
int main() {
int value = 4;
int shifted = value << 1;
printf("Shifted value: %d", shifted);
return 0;
}
Shifted value: 8
The ternary operator provides a compact way to write a simple if-else decision within a single expression. It is the only operator in C that requires exactly three operands, which is where its name comes from.
condition ? valueIfTrue : valueIfFalse
#include <stdio.h>
int main() {
int views = 8000;
char *status = (views > 5000) ? "Trending" : "Growing";
printf("Video status: %s", status);
return 0;
}
Video status: Trending
This single line achieves the same result as a longer if-else block, making the ternary operator useful for short, straightforward decisions that would otherwise require several extra lines of code.
When an expression contains multiple operators, C follows a specific order to decide which operations are performed first, known as operator precedence. When operators share the same precedence level, associativity determines whether evaluation proceeds from left to right or right to left.
| Concept | Explanation |
|---|---|
| Precedence | Determines which operator is evaluated first when multiple different operators appear in the same expression. |
| Associativity | Determines the evaluation order when operators of equal precedence appear together, typically left to right for most operators. |
| Parentheses | Can be used to override default precedence and make the intended order of evaluation explicit. |
#include <stdio.h>
int main() {
int result = 10 + 5 * 2;
int resultWithParentheses = (10 + 5) * 2;
printf("Without parentheses: %d", result);
printf("\nWith parentheses: %d", resultWithParentheses);
return 0;
}
Without parentheses: 20 With parentheses: 30
Because multiplication has higher precedence than addition, the first expression evaluates the multiplication before the addition, while adding parentheses around the addition forces it to be evaluated first instead.
| Mistake | Correct Practice |
|---|---|
| Confusing the assignment operator with the equality operator. | Use a single equals sign only for assignment and a double equals sign only for comparison. |
| Expecting decimal results from dividing two integers. | Cast at least one operand to a floating-point type when a decimal result is required. |
| Misusing bitwise operators in place of logical operators. | Use logical AND and OR for combining boolean-style conditions, reserving bitwise operators for bit manipulation. |
| Ignoring operator precedence in complex expressions. | Use parentheses to explicitly control the order of evaluation whenever there is any doubt. |
Operators are the building blocks that let a C program actually manipulate the data stored in its variables, from simple arithmetic calculations to complex conditional logic involving multiple comparisons. By learning arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators, you gain the tools needed to express almost any calculation or decision a program might require.
In this tutorial, you explored each major category of operator in C, saw practical examples of how they behave, and learned how operator precedence determines the order of evaluation in more complex expressions. With operators firmly understood, you are now ready to explore how input and output work in C, allowing your programs to interact directly with users.