Values, types, and expressions
Choose C data types, evaluate expressions using precedence, and control implicit or explicit conversions.
After this lesson
You should be able to
- Select an appropriate basic type for a value and operation.
- Predict an expression's result without relying on visual guesswork.
A type is a contract
A variable has a type that determines how its bits are interpreted and which operations are meaningful. int is used for whole-number arithmetic, char for character-sized integer data, and float or double for fractional values. Exact ranges depend on the implementation, so use limits.h when limits matter.
Scope tells where a name is visible; storage duration tells how long its object exists. A local automatic variable normally lives for one block call, while a static local retains its value between calls. Do not use static merely to avoid understanding data flow.
Evaluate, then convert
Multiplication and division bind more tightly than addition and subtraction. Parentheses communicate intention and should be used when an expression is not immediately obvious. Relational operators produce 0 or 1 in C, and logical operators treat zero as false and nonzero as true.
In integer division, 5 / 2 is 2. Assigning that result to double gives 2.0, because the information was already lost. Write 5.0 / 2 or cast before division when you need 2.5.
int total = 5;
int count = 2;
double wrong = total / count; /* 2.0 */
double right = (double) total / count; /* 2.5 */Try it yourself
Predict x and y: int x = 2 + 3 * 4; double y = (2 + 3) / 4.0;
Need a hint?
Apply parentheses first, then multiplication/division, then addition.
Check the worked solution
x is 14 because multiplication happens first. y is 1.25 because the parenthesized sum is divided by floating-point 4.0.
Quick check
What is the value of double result = 7 / 2;?
Why this lesson exists
Syllabus mapping
Variables and data types · Operators · Precedence · Expression evaluation · Storage classes · Type conversion
Maps to course outcomes CO2, CO3.