Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Operators are the verbs of C programming—they're how you actually do things with your data. Every calculation, every comparison, every decision your program makes relies on operators. You're being tested not just on recognizing operator symbols, but on understanding operator precedence, type behavior (like integer division truncation), and side effects (how increment operators change values mid-expression). These concepts appear constantly in exam questions that ask you to trace code or predict output.
Mastering operators means understanding evaluation order, type conversion, and memory manipulation. The difference between = and == has crashed countless programs, and knowing when to use && versus & separates competent programmers from confused ones. Don't just memorize symbols—know what each operator returns, how it affects its operands, and where precedence might trip you up.
These operators handle the fundamental math your programs need. The key insight: C treats integer and floating-point arithmetic differently, and this catches many students off guard.
+, subtraction -, multiplication *, division /, and modulus % form the foundation of all calculations++x and x++ both add 1, but timing differs based on prefix vs. postfix formy = ++x, x increments first, then y gets the new value; postfix (x++) assigns first, then incrementsfor loop uses these operators; understanding prefix vs. postfix prevents off-by-one errorsCompare: ++x vs. x++—both increment x by 1, but prefix returns the new value while postfix returns the original value. If asked to trace y = x++ where x starts at 5, y becomes 5 and x becomes 6.
Assignment operators store values in variables. The critical distinction: assignment is an operation that returns a value, not just a statement.
a = b = c = 0, which assigns 0 to all three variables= (assignment) with == (comparison) compiles without error but produces bugs; if (x = 5) always evaluates truex += 5 is equivalent to x = x + 5, reducing repetition and potential typos+=, -=, *=, /=, %=, and even bitwise versions like &= and |=arr[i++] += 1 only increments i onceCompare: x = x + 1 vs. x += 1 vs. x++—all increment x by 1, but compound assignment and increment operators are more concise and, in some contexts, can be more efficient. Use ++ for simple increments, += when adding values other than 1.
Relational and logical operators power every if statement and loop condition. These operators always return either 0 (false) or 1 (true) in C.
==, not equal !=, less than <, greater than >, less than or equal <=, greater than or equal >=x + 1 < y evaluates the addition before the comparison; parentheses clarify intent&& requires both operands true; OR || requires at least one true; NOT ! inverts the value&& stops if the left operand is false; || stops if the left operand is true; this prevents unnecessary computation and potential errors!x && y applies NOT to x only; use parentheses !(x && y) to invert the entire expressionCompare: && vs. &—logical AND (&&) works on truth values and short-circuits; bitwise AND (&) operates on individual bits and always evaluates both sides. Using & when you mean && is a common bug that sometimes works by accident.
Bitwise operators manipulate individual bits within integers. These are essential for systems programming, embedded development, and optimization.
& yields 1 only where both bits are 1; OR | yields 1 where either bit is 1; XOR ^ yields 1 where bits differ~ flips every bit; ~0 becomes all 1s (typically in two's complement)x << n is equivalent to ; bits shift left, zeros fill from the rightx >> n is equivalent to for unsigned types; behavior varies for signed negativesCompare: << vs. *—left-shifting by 1 (x << 1) doubles x just like x * 2, but shifting is a single CPU instruction. For powers of 2, shifting is idiomatic in systems code; for general multiplication, use * for clarity.
These operators serve specific roles that don't fit neatly into other categories. Each solves a particular programming problem elegantly.
condition ? value_if_true : value_if_false; the only C operator taking three operandsmax = (a > b) ? a : b assigns directlysizeof(int) gives the byte count for an int on your system (commonly 4 bytes)sizeof(x) returns the size of variable x; parentheses optional for variables but required for type namesmalloc(n * sizeof(int)) ensures portable, correct memory requests regardless of platform(a = 1, b = 2, a + b), all three execute but the expression yields for (i = 0, j = 10; i < j; i++, j--) initializes and updates multiple variablesCompare: Conditional ? : vs. if-else—both make decisions, but the ternary operator is an expression returning a value while if-else is a statement. Use ternary for simple value selection; use if-else for complex actions or multiple statements.
Pointer operators give you direct access to memory addresses. Understanding these is crucial for dynamic memory, arrays, and efficient function calls.
&x returns the memory address where variable x is stored; this address can be stored in a pointerp holds an address, *p accesses the value at that address; also used in pointer declarations (int *p)Compare: * in declarations vs. expressions—int *p declares p as a pointer to int; *p = 5 dereferences p to store 5 at the pointed-to location. Same symbol, different contexts—this trips up many beginners.
| Concept | Best Examples |
|---|---|
| Basic math operations | +, -, *, /, % |
| Value modification | =, +=, -=, ++, -- |
| Comparisons | ==, !=, <, >, <=, >= |
| Boolean logic | &&, ` |
| Bit manipulation | &, ` |
| Conditional expressions | ? : |
| Memory and size | sizeof, *, & |
| Multiple expressions | , (comma operator) |
What is the output of int x = 5; int y = x++; for both x and y? How would the result differ with ++x?
Why does 7 / 2 evaluate to 3 in C, and what are two ways to get the result 3.5 instead?
Compare && and &: when would using the wrong one cause a bug, and when might it accidentally work correctly?
If int a = 10; and int *p = &a;, what do p, *p, and &a each represent? How would you use these to change a's value to 20?
Trace this expression: int result = (3 > 2) ? (4 + 1) : (4 - 1); What value does result hold, and how would you rewrite this using an if-else statement?