Operators, Expressions & Preprocessor
Expressions combine values with operators. Precedence decides what binds first, and integer division decides whether you get a fraction or a truncation.
In plain English
Operators are tools. Precedence is the rule for which tool to use first, like multiplication before addition.
Arithmetic, precedence, and division
Multiplication and division bind before addition and subtraction. Parentheses override precedence. Integer division truncates toward zero, so 5 / 2 is 2. Cast one operand to double when you need a fractional result. The modulo operator % gives the remainder and works only on integers.
int a = 5, b = 2;
printf("%d\n", a / b); /* 2 */
printf("%.2f\n", (double)a / b); /* 2.50 */
printf("%d\n", a % b); /* 1 */Preprocessor and translation
The preprocessor runs before compilation. #include pastes a header into the translation unit, #define performs token substitution, and conditional directives such as #ifdef include or exclude code. The compiler translates the source into an object file, and the linker combines object files into an executable. Understanding this pipeline explains why a missing prototype or header can fail at different stages.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
int main(void) {
printf("%d\n", SQUARE(5));
return 0;
}Key takeaways
- Multiplicative operators bind before additive operators.
- Integer division truncates; cast to double for a fraction.
- % is the integer remainder operator.
- The preprocessor handles includes, macros, and conditional compilation before the compiler.
Common pitfalls
- Expecting 5 / 2 to be 2.5.
- Forgetting parentheses in a macro, causing precedence bugs.
- Using % with floating-point operands.
Key focus
- Trace expressions with precedence and parentheses.
- Expect questions on integer division and macro expansion.
Quick check
1. What is the value of 7 / 2 in C when both operands are int?
2. Which stage runs first: the preprocessor or the compiler?