1. Introduction: The Heart of Computation in C
Variables and memory buffers don't do much on their own. In C, operators are the actual workhorses — whether you're shifting bits into a peripheral register, walking an array with pointer arithmetic, or evaluating branch conditions in a tight loop.
An expression is simply any valid sequence of operators, variables, and constants that the compiler can evaluate down to a single value.
It sounds straightforward, but because C gives you direct memory access without safety rails, subtle misunderstandings around operator precedence, sequence points, or implicit type promotion are responsible for some of the hardest-to-find bugs in systems software.
/* A real-world style expression combining arithmetic, relational, bitwise, and ternary operators */
int result = (offset + stride * 2 > limit) ? (status & MASK_READY) : (val << 1);
2. Deep Dive: The 8 Categories of C Operators
C groups its built-in operators into eight functional families:

1. Arithmetic Operators
Arithmetic operators handle standard math on integer and floating-point types.
| Operator | Name | Operation | Example (a = 10, b = 3) | Result |
| :---: | :--- | :--- | :--- | :---: |
| + | Addition | Adds two values | a + b | 13 |
| - | Subtraction | Subtracts right operand from left | a - b | 7 |
| * | Multiplication | Multiplies two values | a * b | 30 |
| / | Division | Divides numerator by denominator | a / b | 3 (Integer truncation) |
| % | Modulo | Yields the remainder after division | a % b | 1 |
Things That Catch People Off Guard:
- Integer Division Truncates Toward Zero:
When both operands are integers,
/drops any fractional remainder without rounding.7 / 2gives3, not3.5. To preserve precision, cast at least one operand to afloatordouble:(float)7 / 2evaluates to3.5f. - Modulo Only Works on Integers:
The
%operator strictly requires integer types (int,char,short,long). If you pass floating-point numbers like7.5 % 2.1, the compiler refuses to compile it. Usefmod()from<math.h>when you need a floating-point remainder. - The Sign of Modulo (
a % b): Prior to C99, integer division with negative numbers was implementation-defined. Under modern ISO C (C99, C11, C17, C23), integer division is strictly required to truncate toward zero. As a result, the remainder always takes the sign of the dividend (a):-10 % 3evaluates to-1(because-10 / 3is-3, and-10 - (3 * -3) = -1).10 % -3evaluates to1.
2. Relational Operators
Relational operators compare two quantities and return a boolean status: 1 for true, 0 for false.
| Operator | Comparison | Example (x = 5, y = 10) | Result |
| :---: | :--- | :--- | :---: |
| < | Strictly less than | x < y | 1 (True) |
| <= | Less than or equal to | x <= 5 | 1 (True) |
| > | Strictly greater than | x > y | 0 (False) |
| >= | Greater than or equal to | y >= 10 | 1 (True) |
| == | Exactly equal to | x == y | 0 (False) |
| != | Not equal to | x != y | 1 (True) |
Common Bug: Chained Comparison Expressions
In math, you can write $1 < x < 10$ to check if $x$ falls inside a range. If you write that in C, it will compile, but it won't do what you think:
C evaluates left-to-right: (1 < x) < 10.
If x = 50, 1 < 50 evaluates to 1 (true). Then 1 < 10 evaluates to 1 (true). The check silently passes even though $50 > 10$.
/* WRONG — passes for x = 50! */
if (1 < x < 10) { ... }
/* CORRECT — explicit logical conjunction */
if (1 < x && x < 10) { ... }
3. Logical Operators
Logical operators connect conditional tests to build decision trees.
| Operator | Name | Logic |
| :---: | :--- | :--- |
| && | Logical AND | Returns 1 only if both sides are non-zero |
| \|\| | Logical OR | Returns 1 if at least one side is non-zero |
| ! | Logical NOT | Inverts truth: turns 0 into 1, and non-zero into 0 |
The Short-Circuit Guarantee
C compilers guarantee short-circuit evaluation for && and ||. If the outcome is decided by the first operand, the second operand is never evaluated at all:
- For
&&: If the left operand is0(false), the entire expression must be false. The right side is skipped. - For
||: If the left operand is non-zero (true), the entire expression must be true. The right side is skipped.
This is the standard idiom for protecting against null pointer dereferences:
int *buffer = get_stream_buffer();
/* Safe: If buffer is NULL, the second condition (*buffer == HEADER_SYNC) is never evaluated */
if (buffer != NULL && *buffer == HEADER_SYNC) {
process_packet(buffer);
}
Rule of thumb: Never put function calls with critical side effects (like
i++orwrite_log()) on the right-hand side of&&or||, because you can't be sure they'll run.
4. Assignment & Compound Operators
The assignment operator = copies the evaluated value from the right-hand side into the memory location on the left.
- The left side must be a modifiable lvalue (a variable, array element, or dereferenced pointer).
- The right side can be any valid expression (rvalue).
Compound Assignments
C provides shorthand operators (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=) that avoid repeating the target variable:
var op= expr is evaluated as var = var op (expr)
Notice the implied parentheses around expr. This matters:
int y = 3;
int x = 10;
x *= y + 2; // This is x = x * (y + 2) => 10 * 5 = 50
// It is NOT x = x * y + 2 (which would have been 32)
5. Increment (++) and Decrement (--) Operators
These unary operators modify an integer or pointer variable by adding or subtracting one.
- Prefix (
++x,--x): Modifies the variable first, then yields the new value. - Postfix (
x++,x--): Yields the current value first, then applies the change.
int a = 5;
int b = ++a; // a is now 6, b gets 6
int x = 5;
int y = x++; // y gets 5, then x becomes 6
The Trap: Unsequenced Modifications
Avoid writing code that modifies the same variable more than once in an expression without an intervening sequence point:
/* UNDEFINED BEHAVIOR: Never do this */
int i = 5;
int val = i++ + ++i;
Different compilers (or even different optimization flags like -O0 vs -O3) will evaluate this to completely different numbers because the order of side-effects is explicitly unsequenced by the C standard. Keep increments on their own lines.
6. Ternary Conditional Operator (? :)
The ternary operator is C's only three-operand operator. It's essentially an inline if-else that evaluates to an expression rather than a statement:
int max = (a > b) ? a : b;
It works well for clean, compact decisions, but nesting them quickly destroys readability:
/* Clean and readable */
const char *label = (status == 0) ? "OK" : "ERROR";
/* Hard to parse — use a switch or if/else instead */
int val = a ? b ? c : d : e ? f : g;
7. Bitwise Operators
Bitwise operators manipulate raw bits in integer types (char, uint8_t, int, uint32_t, etc.). They are the foundation of device drivers, network protocols, cryptography, and embedded systems.
| Operator | Name | Effect on Bits |
| :---: | :--- | :--- |
| & | Bitwise AND | Bit is 1 only if both inputs are 1 |
| \| | Bitwise OR | Bit is 1 if either input is 1 |
| ^ | Bitwise XOR | Bit is 1 if the inputs are different |
| ~ | Bitwise NOT | Flips every bit (1's complement) |
| << | Left Shift | Moves bits left; new lowest bits are filled with 0s |
| >> | Right Shift | Moves bits right |
Essential Bit Manipulation Recipes
Here are the four bitwise operations you will use constantly in C:
/* 1. Set the k-th bit */
flags |= (1U << k);
/* 2. Clear the k-th bit */
flags &= ~(1U << k);
/* 3. Toggle the k-th bit */
flags ^= (1U << k);
/* 4. Test if the k-th bit is set */
bool is_set = (flags & (1U << k)) != 0;
Shift Math & Gotchas:
- Left shift (
x << n): For unsigned numbers, equivalent to multiplying by $2^n$ (assuming no overflow).5U << 2gives $5 \times 4 = 20$. - Right shift (
x >> n): For unsigned numbers, equivalent to integer division by $2^n$.20U >> 2gives $20 / 4 = 5$. - Warning on signed shifts: Right-shifting a negative signed integer (e.g.
(-8) >> 1) is implementation-defined in C (most modern compilers perform an arithmetic shift, preserving the sign bit). Left-shifting a negative number or shifting by $\ge$ the bit width of the type is undefined behavior. Always perform bit operations onunsignedintegers.
8. Special Operators
-
sizeof: Evaluates the memory footprint of a type or variable in bytes at compile time.Snippetint arr[10]; size_t total_bytes = sizeof(arr); // 40 bytes (assuming 4-byte int) size_t length = sizeof(arr) / sizeof(arr[0]); // 10 elementsNote:
sizeofis a built-in operator, not a runtime function. The parentheses are mandatory for type names (sizeof(int)), but optional for variables (sizeof arr). -
The Comma Operator (
,): Chains expressions together left-to-right, discarding all results except the last one.Snippet/* Evaluates a=3, then b=5, then computes a + b (8) into x */ int x = (a = 3, b = 5, a + b);In real-world code, you'll mostly see this inside multi-variable
forloops:Snippetfor (int i = 0, j = len - 1; i < j; i++, j--) { swap(&arr[i], &arr[j]); } -
Pointer & Member Operators:
&Address-of operator (gets memory address).*Dereference operator (accesses data at pointer address)..Direct member access on structs (sensor.reading).->Indirect member access through pointer (sensor_ptr->reading).
3. Type Conversion Flow & Evaluation Rules
When an expression mixes types (like adding a short to a double), C automatically promotes operands upward to a common denominator so calculations don't lose precision.

The Usual Arithmetic Conversions
- Integer Promotions: Any
charorshortoperand is promoted toint(orunsigned int) before any math happens. - Rank Hierarchy: Operands are converted upward to match the highest-ranking type in the expression:
char, short -> int -> unsigned int -> long -> unsigned long -> long long -> unsigned long long -> float -> double -> long double
The Signed / Unsigned Comparison Bug
This is one of the classic ways C code breaks in production:
int a = -1;
unsigned int b = 1;
if (a < b) {
printf("Expected: -1 is less than 1\n");
} else {
printf("Bug triggered!\n");
}
If you run this code, it prints "Bug triggered!".
Why? Because a (int) is mixed with b (unsigned int). C promotes a to unsigned int. In 32-bit two's complement, -1 represented as an unsigned integer is 4,294,967,295 (UINT_MAX), which is obviously greater than 1!
Always be cautious when comparing signed and unsigned variables. Turn on compiler warnings like -Wsign-compare and -Wconversion to catch these automatically.
4. Operator Precedence & Associativity Hierarchy
When an expression contains multiple operators, C uses precedence to decide which operator binds to operands first, and associativity to determine the direction of evaluation when operators share the same tier.
| Level | Family | Operators | Description | Associativity |
| :---: | :--- | :--- | :--- | :--- |
| 1 | Postfix / Member | () [] . -> ++ -- | Calls, indexing, member access, postfix inc/dec | Left-to-Right |
| 2 | Unary | + - ! ~ ++ -- (type) * & sizeof | Negation, logical/bitwise NOT, casts, dereference | Right-to-Left |
| 3 | Multiplicative | * / % | Multiplication, division, modulo | Left-to-Right |
| 4 | Additive | + - | Addition and subtraction | Left-to-Right |
| 5 | Bitwise Shift | << >> | Bit shifts | Left-to-Right |
| 6 | Relational | < <= > >= | Comparison bounds | Left-to-Right |
| 7 | Equality | == != | Equality tests | Left-to-Right |
| 8 | Bitwise AND | & | Bitwise masking | Left-to-Right |
| 9 | Bitwise XOR | ^ | Bitwise toggle | Left-to-Right |
| 10 | Bitwise OR | \| | Bitwise combine | Left-to-Right |
| 11 | Logical AND | && | Boolean AND (short-circuiting) | Left-to-Right |
| 12 | Logical OR | \|\| | Boolean OR (short-circuiting) | Left-to-Right |
| 13 | Conditional | ? : | Ternary decision | Right-to-Left |
| 14 | Assignment | = += -= *= /= %= &= \|= ^= <<= >>= | Simple and compound stores | Right-to-Left |
| 15 | Comma | , | Sequencing separator | Left-to-Right |
5. Practical Expression Walkthrough
Let's trace how the compiler dismantles a multi-operator expression step-by-step:
Given:
int a = 10, b = 5, c = 2;
int result = a + b * c > 12 && b - c != a / c;
Step-by-Step Breakdown:
- Multiplication & Division (Tier 3):
b * c$\rightarrow 5 \times 2 = 10$a / c$\rightarrow 10 / 2 = 5$- Expression is now:
a + 10 > 12 && b - c != 5
- Addition & Subtraction (Tier 4):
a + 10$\rightarrow 10 + 10 = 20$b - c$\rightarrow 5 - 2 = 3$- Expression is now:
20 > 12 && 3 != 5
- Relational Comparison (Tier 6):
20 > 12evaluates to1(true)- Expression is now:
1 && 3 != 5
- Equality Comparison (Tier 7):
3 != 5evaluates to1(true)- Expression is now:
1 && 1
- Logical AND (Tier 11):
1 && 1evaluates to1
- Assignment (Tier 14):
result = 1
Final Value: result = 1.
6. Practical Takeaways for Daily C Programming
- When in doubt, use parentheses: Nobody has all 15 precedence levels memorized, and your teammates shouldn't have to guess. Writing
(a & MASK) != 0is always better than relying on!=binding tighter than&. - Never depend on side effects across
&&or||: Because of short-circuiting, any function calls or increments on the right side might never execute. - Keep increments on their own lines: Avoid combining
++or--inside arithmetic expressions or function arguments. It invites undefined behavior. - Beware of mixed signed/unsigned comparisons: Compilers silently promote signed integers to unsigned values, which turns small negative values into massive positive ones.