Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 03 / 21 · THE LANGUAGE

Expressions and conversions.

Follow arithmetic, comparisons, integer division, precedence, and overflow.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

An expression computes a value and may also have a side effect, such as changing an object. Arithmetic operators, comparisons, logical operators, assignment, and function calls all participate in expressions. The type of each operand affects the result: dividing two integers produces an integer result, even if a mathematically exact answer would have a fraction. C does not infer that you intended floating-point arithmetic from the destination variable.

Conversions often happen automatically. Integer promotions may turn small integer types into int before arithmetic; the usual arithmetic conversions then find a common type for operands. Mixed signed and unsigned arithmetic can therefore produce a result that surprises a reader. Rather than memorizing one slogan, write down the type of each subexpression and check the range of possible values.

02 / THE RULES

What C guarantees.

Precedence controls grouping, while evaluation order controls when operands and side effects occur. These are not the same. Parentheses can make grouping explicit but do not impose a universal left-to-right evaluation order on function arguments. Avoid expressions that modify one object in several unsequenced places; separate the work into statements so the intended order is visible.

Signed integer overflow has undefined behavior in C. Unsigned arithmetic instead wraps modulo its type's range, which is sometimes useful but often accidental. Check a boundary before arithmetic when input may be large. The logical && and || operators short-circuit, so the right operand is evaluated only when needed; that makes them useful for guarding a pointer or denominator before a risky operation.

03 / UNDER THE SURFACE

Why the detail matters.

The apparent mathematical formula and the actual C expression can differ because each operator has typed operands. In a / b, if both operands are integers, the fractional part is discarded toward zero before the result is assigned anywhere. Casting one operand before division changes that operation; casting the already divided result afterward does not restore lost information. The same timeline explains why a comparison between signed and unsigned values can surprise: conversion may happen before the comparison, not after it.

Sequencing is about when side effects become ordered relative to other evaluations. A program that writes the same scalar twice in one unsequenced expression may have undefined behavior, even if one compiler consistently produces a plausible number. Split such work into named statements. The extra lines show the intended order, make bounds checks possible before arithmetic, and let a reader inspect intermediate types without decoding a dense expression.

04 / CODE, EXPLAINED

See the rule in context.

expressions-and-conversions.c
#include <stdio.h>

int main(void) {
    int completed = 7;
    int planned = 10;
    if (planned == 0) return 1;
    double ratio = (double)completed / planned;
    if (ratio >= 0.5) {
        printf("At least halfway: %.1f%%\n", ratio * 100.0);
    }
    return 0;
}
FOLLOW THE EXECUTION

The program checks planned before using it as a divisor. Casting completed to double makes the division floating-point, so ratio becomes 0.7. Because ratio is at least 0.5, the body runs. The format %.1f prints one digit after the decimal point; %% prints a literal percent sign. The output is At least halfway: 70.0%.

05 / THE BOUNDARY

Where the rule stops.

A later condition cannot retroactively protect an earlier division. With external input, check the denominator before dividing and check that the resulting ratio has the meaning your domain expects.

06 / THE CONNECTION

Keep this model.

C expressions are strongest when they are small enough to trace. The next chapter uses those expressions as conditions and loop updates, where a clear sequence of state changes matters more than compact punctuation.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.