Operators in C are symbols that tell the compiler to perform specific operations on data. These operations may involve addition, subtraction, comparison, logical checking, bit manipulation, assignment, or several other actions. Operators are used in almost every C program because programs constantly need to calculate values, compare conditions, update variables, and control execution flow.
This topic is one of the core foundations of C programming. If you understand operators properly, expressions, conditions, loops, assignments, and even many advanced topics become much easier. In this article, we will learn what operators in C are, what operands are, the main categories of operators, practical examples, precedence rules, and common mistakes beginners should avoid.
What are Operators in C?
Operators in C are special symbols that perform operations on one or more operands. An operand is the data on which the operator works. For example, in the expression a + b, the symbol + is the operator, and a and b are operands.
Operators are not all the same. Some operators work with one operand, some with two, and a few with three parts. This is why operators are often discussed not only by purpose but also by how many operands they use.
| Term | Meaning | Example |
|---|---|---|
| Operator | Symbol that performs an operation | +, ==, && |
| Operand | Value or variable used by the operator | a, 10, marks |
| Expression | Combination of operands and operators | a + b |
Types of Operators in C
C provides several categories of operators. Each category solves a different kind of programming problem.
| Category | Main Purpose |
|---|---|
| Arithmetic operators | Perform mathematical calculations |
| Relational operators | Compare values |
| Logical operators | Combine or invert conditions |
| Assignment operators | Assign or update values |
| Increment and decrement operators | Increase or decrease a value by one |
| Bitwise operators | Manipulate data at the bit level |
| Conditional operator | Select between two values using a condition |
sizeof operator | Return the size of a type or object in bytes |
| Comma operator | Evaluate expressions in sequence |
Arithmetic Operators in C
Arithmetic operators are used to perform mathematical operations. These are the operators beginners use first because they are close to normal school mathematics.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder (modulus) | a % b |
One important point in C is that integer division does not produce a decimal result. If both operands are integers, the fractional part is discarded.
#include <stdio.h>
int main(void)
{
int a = 10;
int b = 3;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}In this example, 10 / 3 gives 3, not 3.333..., because both operands are integers.
Relational Operators in C
Relational operators compare two values. They are commonly used in conditions with if, while, and for statements. The result of a relational expression is logically true or false.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
These operators are essential in decision-making because they let the program test relationships between values.
Logical Operators in C
Logical operators are used to combine or invert conditions. They are especially useful when a decision depends on more than one comparison.
| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
If you write (age >= 18 && age <= 60), the full condition becomes true only when both comparisons are true. Logical operators make complex conditions possible without deeply nested statements.
C also uses short-circuit evaluation for && and ||. That means the second part may not be evaluated if the first part already determines the result. This is useful, but beginners should still write conditions carefully.
Assignment Operators in C
Assignment operators store a value in a variable. The simple assignment operator is =, but C also provides compound assignment operators that combine an operation with assignment.
| Operator | Meaning | Equivalent Form |
|---|---|---|
= | Assign value | a = b |
+= | Add and assign | a = a + b |
-= | Subtract and assign | a = a - b |
*= | Multiply and assign | a = a * b |
/= | Divide and assign | a = a / b |
%= | Modulus and assign | a = a % b |
Compound assignment operators make code shorter and usually clearer when the same variable is being updated repeatedly.
Increment and Decrement Operators in C
The increment operator ++ increases a variable by one, and the decrement operator -- decreases a variable by one. These operators are heavily used in loops and counters.
| Operator | Meaning | Example |
|---|---|---|
++ | Increase by 1 | i++ or ++i |
-- | Decrease by 1 | i-- or --i |
There are two forms:
- Prefix form:
++ior--iupdates the value before it is used. - Postfix form:
i++ori--uses the current value first and then updates it.
This difference matters in expressions, so beginners should not treat prefix and postfix as always interchangeable.
#include <stdio.h>
int main(void)
{
int i = 5;
int a = ++i;
int b = i++;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("i = %d\n", i);
return 0;
}Bitwise Operators in C
Bitwise operators work directly on the binary representation of integers. These operators are especially important in embedded systems, low-level programming, hardware control, and flag handling.
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
For example, left shift often moves bits toward higher positions and can resemble multiplication by powers of two in many simple cases. Right shift often resembles division by powers of two for non-negative values, but bitwise behavior should always be understood carefully rather than memorized blindly.
Conditional Operator in C
The conditional operator ?: is sometimes called the ternary operator because it works with three parts. It is a compact way to select one of two values based on a condition.
General form:
condition ? value_if_true : value_if_false;Example:
int max = (a > b) ? a : b;This operator is useful when the decision is simple and the expression remains readable.
sizeof Operator in C
The sizeof operator returns the size of a type or object in bytes. It is commonly used when working with memory, arrays, structures, and dynamic allocation.
#include <stdio.h>
int main(void)
{
int num;
printf("Size of int: %zu bytes\n", sizeof(num));
printf("Size of double: %zu bytes\n", sizeof(double));
return 0;
}This operator is very useful because type sizes can vary across systems. Using sizeof is much safer than assuming every type always uses the same amount of memory.
Comma Operator in C
The comma operator evaluates expressions from left to right and returns the value of the last expression. It is valid in C, but beginners should use it carefully because it can reduce readability if overused.
Example:
int x = (a = 5, a + 2);In this example, a = 5 is evaluated first, and then a + 2 becomes the final value of the expression.
Unary, Binary and Ternary Operators in C
Operators can also be grouped by the number of operands they use.
| Type | Operands Used | Examples |
|---|---|---|
| Unary operator | One operand | ++i, --i, !flag, sizeof(x) |
| Binary operator | Two operands | a + b, x == y, m & n |
| Ternary operator | Three parts | condition ? x : y |
This classification helps you understand why some operators behave differently from others.
Operator Precedence and Associativity in C
When an expression contains multiple operators, C follows precedence and associativity rules to decide the order of evaluation. Precedence tells which operator is considered first. Associativity decides the direction of evaluation when operators share the same precedence level.
For example, in the expression 10 + 5 * 2, multiplication happens before addition because * has higher precedence than +.
| Operator Group | Associativity |
|---|---|
Postfix operators such as (), [], ++, -- | Left to right |
Unary operators such as !, ~, prefix ++, prefix --, sizeof | Right to left |
*, /, % | Left to right |
+, - | Left to right |
| Relational and equality operators | Left to right |
Logical operators && and || | Left to right |
Conditional operator ?: | Right to left |
| Assignment operators | Right to left |
| Comma operator | Left to right |
Even when you know precedence rules, using parentheses is often the better choice if an expression may confuse the reader.
Common Mistakes with Operators in C
- Using
=instead of==in conditions - Forgetting that integer division removes the fractional part
- Misunderstanding prefix and postfix increment
- Writing expressions without considering precedence
- Using bitwise operators when logical operators are actually needed
- Overusing complex expressions that reduce readability
Many bugs in beginner C programs come from operator confusion, not from advanced language features. That is why this topic deserves careful practice.
Best Practices for Using Operators in C
- Prefer clarity over compact but confusing expressions.
- Use parentheses when the intended evaluation order might not be obvious.
- Be careful with integer division and modulus.
- Do not mix logical and bitwise operators without understanding the difference.
- Use simple expressions in conditions whenever possible.
- Treat prefix and postfix increment as meaningfully different in expressions.
FAQs
What are operators in C?
Operators in C are symbols that perform operations on data, such as arithmetic calculations, comparisons, logical evaluation, assignment, and bit manipulation.
What is the difference between arithmetic and relational operators in C?
Arithmetic operators perform calculations like addition and subtraction, while relational operators compare values and produce logical true or false results.
What is the difference between = and == in C?
= is the assignment operator used to store a value in a variable. == is the equality operator used to compare two values.
What are bitwise operators used for in C?
Bitwise operators are used for low-level binary operations, flag control, hardware-related programming, and efficient data manipulation at the bit level.
Why is operator precedence important in C?
Operator precedence determines the order in which parts of an expression are evaluated. Without understanding it, a programmer may get unexpected results.