Operators in C Programming Language
A Comprehensive Report
1. Introduction
An operator in C is a symbol that tells the compiler to perform a specific operation on one or more
operands. Operators are fundamental to C programming and are used in virtually every program to
perform calculations, comparisons, logical decisions, and memory manipulation.
C provides a wide variety of operators that can be grouped into the following categories:
arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional (ternary),
and special operators such as sizeof and the comma operator. Each category serves a distinct
purpose and is suited to particular programming scenarios.
Understanding how operators work, including the rules of precedence and associativity that govern
their evaluation order, is essential for writing correct and efficient C programs. This report covers
all major operator types with syntax, examples, and supporting tables.
2. Arithmetic Operators
Arithmetic operators perform basic mathematical calculations. They work on numeric data types
such as int, float, double, and char. C supports both binary arithmetic operators (requiring two
operands) and unary operators like increment and decrement (requiring one operand).
Operator Name Example Result / Description
+ Addition a+b Sum of a and b
- Subtraction a-b Difference of a and b
* Multiplication a*b Product of a and b
/ Division a/b Quotient (truncated for integers)
% Modulus a%b Remainder of a / b
++ Increment a++/++a Increases value by 1
-- Decrement a--/--a Decreases value by 1
Note that integer division in C truncates the fractional part. For example, 7 / 2 gives 3, not 3.5. To
get a floating-point result, at least one operand must be cast to float or double. Similarly, the
modulus operator (%) is only valid for integer operands.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d\n", a + b); // 13
printf("%d\n", a - b); // 7
printf("%d\n", a * b); // 30
printf("%d\n", a / b); // 3 (integer division)
printf("%d\n", a % b); // 1
return 0;
}
3. Relational Operators
Relational operators compare two values and return an integer result: 1 (true) or 0 (false). They are
used primarily in control flow statements like if, while, and for to make decisions based on
comparisons between values.
Operator Meaning Example Returns
== Equal to a == b 1 if true, 0 if false
!= Not equal to a != b 1 if true, 0 if false
> Greater than a>b 1 if true, 0 if false
< Less than a<b 1 if true, 0 if false
>= Greater than or equal a >= b 1 if true, 0 if false
<= Less than or equal a <= b 1 if true, 0 if false
A very common mistake among beginners is using = (assignment) instead of == (equality) inside a
condition. For example, if (a = 5) always evaluates to true because the assignment itself returns 5.
Most compilers will warn about this, but it is still a frequent source of bugs.
4. Logical Operators
Logical operators are used to combine or negate conditional expressions. They return 1 (true) or 0
(false) and are essential in writing compound conditions.
Operator Name Example Description
&& Logical AND a && b True if both operands are non-zero
|| Logical OR a || b True if at least one operand is non-zero
! Logical NOT !a Reverses the logical state of the operand
An important feature of && and || is short-circuit evaluation. For &&, if the left operand is false,
the right operand is not evaluated at all because the result is already known to be false. For ||, if the
left operand is true, the right operand is skipped. This behavior can be used to prevent errors such
as division by zero, as in: if (b != 0 && a / b > 2).
5. Bitwise Operators
Bitwise operators work directly on the binary representation of integers. They are commonly used
in systems programming, embedded development, and situations where memory or performance
efficiency is critical. They operate on each individual bit of the operands.
Operator Name Example Description
& Bitwise AND a&b ANDs each corresponding bit
| Bitwise OR a|b ORs each corresponding bit
^ Bitwise XOR a^b XORs each corresponding bit
~ Bitwise Complement ~a Flips all bits of a
<< Left Shift a << n Shifts bits of a left by n positions
>> Right Shift a >> n Shifts bits of a right by n positions
For example, consider a = 12 (binary: 00001100) and b = 10 (binary: 00001010). Then a & b = 8
(00001000), a | b = 14 (00001110), and a ^ b = 6 (00000110). Left shifting by n positions is
equivalent to multiplying by 2^n, and right shifting by n positions is equivalent to dividing by 2^n
(for positive integers).
6. Assignment Operators
The assignment operator (=) stores the value of the right-hand operand into the left-hand variable.
C also provides compound assignment operators that combine an arithmetic or bitwise operation
with assignment, providing a shorter and often more readable syntax.
Operator Equivalent To Description
= a=b Assigns value of b to a
+= a=a+b Adds b to a and stores in a
-= a=a-b Subtracts b from a and stores in a
*= a=a*b Multiplies a by b and stores in a
/= a=a/b Divides a by b and stores in a
%= a=a%b Stores remainder of a/b in a
&= a=a&b Bitwise AND and assign
|= a=a|b Bitwise OR and assign
^= a=a^b Bitwise XOR and assign
<<= a = a << b Left shift and assign
>>= a = a >> b Right shift and assign
For example, a += 5 is equivalent to writing a = a + 5. Compound operators are not just shorthand
— they express the programmer's intent clearly and can sometimes result in more optimised
machine code.
7. Increment and Decrement Operators
The increment (++) and decrement (--) operators increase or decrease a variable's value by 1. Both
exist in two forms: prefix (++a or --a) and postfix (a++ or a--). The difference lies in the value they
return within a larger expression.
- Prefix form (++a): Increments first, then returns the new value.
- Postfix form (a++): Returns the current value first, then increments.
Example:
int a = 5, b = 5, x, y;
x = ++a; // a becomes 6, x = 6
y = b++; // y = 5, b becomes 6 afterwards
When the return value is not used (e.g., a standalone a++; statement), both forms produce identical
results. However, in expressions where the returned value matters, the distinction is important.
8. Conditional (Ternary) Operator
The conditional operator (?:) is the only ternary operator in C, meaning it takes three operands. It
provides a compact alternative to a simple if-else statement and can be used directly inside
expressions.
Syntax:
condition ? expression_if_true : expression_if_false
The condition is evaluated first. If it is non-zero (true), the result is the first expression; otherwise,
the result is the second expression. Only one of the two expressions is evaluated, similar to short-
circuit evaluation.
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b; // max = 20
// Using ternary inside printf
printf("%d is %s", a, (a % 2 == 0) ? "even" : "odd");
While the ternary operator is useful for simple conditional assignments, deeply nested ternary
expressions can reduce readability. For complex conditions, an if-else block is generally preferred.
9. Special Operators
9.1 sizeof Operator
The sizeof operator returns the size in bytes of a data type or variable. It is evaluated at compile
time and is very useful for writing portable code, as data type sizes can vary across platforms.
printf("%zu\n", sizeof(int)); // typically 4
printf("%zu\n", sizeof(double)); // typically 8
int arr[10];
int len = sizeof(arr) / sizeof(arr[0]); // number of elements = 10
9.2 Comma Operator
The comma operator (,) evaluates two expressions left to right and returns the value of the
rightmost one. It is most commonly used in for loop headers to include multiple initializations or
updates.
for (int i = 0, j = 10; i < j; i++, j--)
9.3 Address-of (&) and Dereference (*) Operators
These operators are fundamental to pointer-based programming in C. The address-of operator (&)
returns the memory address of a variable. The dereference operator (*) accesses the value at a given
memory address.
int num = 42;
int *ptr = # // ptr holds the address of num
printf("%d", *ptr); // prints 42 (value at that address)
10. Operator Precedence and Associativity
When an expression contains more than one operator, C evaluates them according to a fixed set of
precedence rules. Operators with higher precedence bind more tightly to their operands and are
evaluated first. When two operators have the same precedence, associativity (left-to-right or right-
to-left) determines the order of evaluation.
Precedence Operators Associativity
1 (Highest) () [] -> . ++ -- Left to Right
2 ++ -- + - ! ~ * & sizeof Right to Left
3 */% Left to Right
4 +- Left to Right
5 << >> Left to Right
6 < <= > >= Left to Right
7 == != Left to Right
8 &|^ Left to Right
9 && || Left to Right
10 ?: Right to Left
11 = += -= *= /= %= &= ^= |= <<= >>= Right to Left
12 (Lowest) , Left to Right
For example, in the expression 5 + 3 * 2, multiplication has higher precedence than addition, so the
result is 5 + 6 = 11, not 16. Similarly, a = b = 5 is evaluated right to left (right-associative), so b is
assigned 5 first, and then a is assigned the result.
It is good practice to use parentheses to make complex expressions clear, even when you are
confident about the default precedence. This improves readability and avoids subtle bugs.
11. Conclusion
Operators are a core part of the C programming language. They allow programmers to perform
arithmetic, compare values, make logical decisions, manipulate bits, manage memory through
pointers, and control the flow of a program. Each category of operator serves a specific purpose,
and together they give C its expressive power.
A solid understanding of all C operators, including their precedence and associativity rules, is
essential for writing correct, efficient, and maintainable programs. Misuse of operators, such as
confusing = with == or misunderstanding prefix versus postfix increment, is a common source of
bugs even for experienced programmers.
By learning operators thoroughly, a programmer builds the foundation needed for advanced topics
in C, including pointer arithmetic, data structures, system calls, and low-level hardware
programming.