1. Introduction
An operator in C is a symbol that instructs the compiler to perform a specific mathematical, relational, or logical operation on one or more operands. Operators, combined with operands, form expressions — the building blocks of every C statement, from a simple sum to a complex conditional check.
Cricket analogy: An operator is like the umpire's raised finger signal that instructs the scorer to perform a specific action, out or not out, on the batter and bowler involved in that delivery.
C classifies operators by the number of operands they act on (unary, binary, ternary) and by the kind of operation they perform: arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and a few special operators like sizeof and the comma operator. This topic surveys everything except bitwise operators, which get their own dedicated, deep-dive page.
Cricket analogy: Classifying operators by operand count is like classifying cricket shots by how many elements combine, a single-handed flick, a two-hand pull shot, or the full sequence of a reverse-sweep, each category needing separate coaching attention, just as bitwise operators get their own dedicated page.
2. Syntax
// General form of an operator expression
operand1 operator operand2; // binary operator, e.g. a + b
operator operand; // unary operator, e.g. -a, ++a
condition ? expr1 : expr2; // ternary (conditional) operator
// Category Symbols
// Arithmetic + - * / %
// Relational == != > < >= <=
// Logical && || !
// Assignment = += -= *= /= %=
// Increment / Decrement ++ --
// Conditional ?:
// Special sizeof , (comma)3. Explanation
Arithmetic operators (+, -, *, /, %) perform standard math on numeric operands. The modulus operator % only works on integer operands and returns the remainder of division; it is undefined for floating-point types. Integer division truncates toward zero, so 7 / 2 evaluates to 3, not 3.5 — a very common source of logic bugs and a favourite exam trap.
Cricket analogy: Modulus only working on integers is like calculating overs remaining, 7 balls left in an over of 6 gives a remainder of 1, never a fraction, and just like integer division truncating, 7 divided by 2 wickets down doesn't give 3.5 wickets, it's simply 3.
Relational operators (==, !=, >, <, >=, <=) compare two operands and produce an int result: 1 for true, 0 for false (C has no dedicated Boolean type before C99's _Bool). Logical operators (&&, ||, !) combine or invert relational expressions and use short-circuit evaluation — in a && b, if a is false, b is never evaluated; in a || b, if a is true, b is never evaluated. This matters when the second operand has side effects, e.g. (p != NULL) && (*p == 5).
Cricket analogy: Relational operators are like DRS returning a simple out/not-out verdict for a comparison, while short-circuit logical evaluation is like an umpire not bothering to check the stumps replay if the no-ball check already failed, since (bowler_legal) && (stumps_broken) skips the second check entirely.
Assignment operators store a value into a variable. Compound assignment operators (+=, -=, *=, /=, %=) are shorthand: a += 5 is equivalent to a = a + 5, but evaluates the left operand only once, which matters for efficiency and for expressions with side effects.
Cricket analogy: Compound assignment like a += 5 is like a scorer adding 5 runs to the total board in one motion instead of reading the current score, computing the sum, then writing it back separately, evaluating the scoreboard reference just once.
Increment (++) and decrement (--) operators add or subtract 1 from a variable. Each has a prefix form (++a) that changes the value before it is used in the enclosing expression, and a postfix form (a++) that uses the current value first and then changes it. Mixing multiple increments/decrements of the same variable in one expression (e.g. a = a++ + ++a;) invokes undefined behaviour in C and must be avoided.
Cricket analogy: Prefix vs postfix increment is like a scorer either updating the run tally before or after announcing it, and mixing multiple such updates for the same delivery in one confusing scorecard entry, like a++ + ++a, would be an undefined mess no scorer should attempt.
The conditional (ternary) operator condition ? expr1 : expr2 is C's only ternary operator and offers a compact alternative to a simple if-else that yields a value. sizeof returns the size in bytes of a type or variable as a size_t, and is evaluated at compile time for most types. The comma operator evaluates left-to-right and yields the rightmost value, commonly seen in for loop headers.
Cricket analogy: The ternary operator is like a quick umpire signal choosing between two outcomes based on one condition, wide-or-not, while sizeof is like measuring a bat's exact weight at compile time, and the comma operator is like a scorer noting multiple events but recording only the final run count.
Operator precedence and associativity decide the order of evaluation when an expression mixes multiple operators. Arithmetic operators bind tighter than relational operators, which in turn bind tighter than logical operators — e.g. a + b > c && d is parsed as ((a + b) > c) && d. When in doubt, use parentheses; they cost nothing and remove ambiguity for both the compiler and the reader.
Do not confuse the assignment operator = with the equality operator ==. Writing if (x = 5) compiles fine, assigns 5 to x, and the condition is always true — a classic bug. Most compilers warn about this with -Wall; treat that warning as an error.
4. Example
#include <stdio.h>
int main(void) {
int a = 10, b = 3;
int sum = a + b;
int rem = a % b;
int isGreater = (a > b); // relational
int logicalCheck = (a > 0) && (b > 0); // logical, short-circuits
a += 5; // compound assignment: a = 15
int pre = ++b; // prefix: b becomes 4, pre = 4
int post = b++; // postfix: post = 4, b becomes 5
int max = (a > b) ? a : b; // ternary
printf("sum = %d, rem = %d\n", sum, rem);
printf("isGreater = %d, logicalCheck = %d\n", isGreater, logicalCheck);
printf("a = %d, pre = %d, post = %d, b = %d\n", a, pre, post, b);
printf("max = %d\n", max);
printf("size of int = %zu bytes\n", sizeof(int));
return 0;
}5. Output
sum = 13, rem = 1
isGreater = 1, logicalCheck = 1
a = 15, pre = 4, post = 4, b = 5
max = 15
size of int = 4 bytes6. Key Takeaways
- Arithmetic operators do standard math; % works only on integers and integer division truncates toward zero.
- Relational operators return 1 (true) or 0 (false); logical && and || short-circuit and may skip evaluating the right operand.
- Compound assignment (+=, -=, ...) evaluates the left operand once, unlike writing the expression out longhand.
- Prefix (++a) and postfix (a++) differ in when the value changes relative to its use; never mix multiple increments of the same variable in one expression.
- The ternary operator
? :is a compact conditional expression that produces a value, unlike if-else which is a statement. - Use parentheses to make precedence explicit — do not rely on memorised precedence tables in real code.
Practice what you learned
1. What is the result of `7 / 2` in C when both operands are int?
2. Which operator category does `&&` belong to?
3. Given `int b = 3; int x = b++ + ++b;`, what can be said about this expression?
4. What does the expression `(x = 5)` evaluate to when used as a condition in `if (x = 5)`?
5. Which operator is C's only ternary (three-operand) operator?
Was this page helpful?
You May Also Like
Bitwise Operators in C
Master C bitwise operators — AND, OR, XOR, NOT, left shift, right shift — with truth tables, precedence, and signed-shift pitfalls.
if-else in C
Learn C's if, if-else, and else-if ladder with syntax, dangling-else pitfalls, nested examples, and exam-style practice questions.
Data Types in C
Master C data types — int, float, char, double, and modifiers — with sizes, ranges, format specifiers, and examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics