An expression has a type and a value (and may have a side effect). A statement does something. n + 1 is an expression. n = n + 1; is a statement.
Mental model: C evaluates operands, then applies the operator. Precedence groups tokens. Sequence points (roughly: ;, &&, ||, ?, function call) decide when side effects settle. Between sequence points, do not modify an object twice or modify it and read it other than to determine the new value.
| Op | Meaning | Notes |
|---|---|---|
+ - * / % | Add, sub, mul, div, remainder | Integer / truncates toward zero (C99). % sign follows the dividend. |
/ by 0 | — | Undefined for integers. IEEE for floats. |
5 / 2 /* 2 */ 5 % 2 /* 1 */ -5 % 2 /* -1 in C99 */ INT_MAX + 1 /* undefined if signed — not a defined wrap */
+ concatenation. Adjacent string literals concatenate in translation: "a" "b" is "ab".+ is scaled by the pointed-to size. See Pointers and Arrays.< > <= >= == != yield int 0 or 1, not a bool type unless you stored them in one.if (n) — nonzero is true. That is not Java. Prefer if (n != 0) when the domain is not a flag.&& and || short-circuit. & and | on integers are bitwise and always evaluate both sides.! is logical not. ~ is bitwise not.if (p != NULL && p->n > 0) { } /* safe */
if (p != NULL & p->n > 0) { } /* UB/crash if p is NULL */
& | ^ ~ << >> on integers. Shifting a negative value or shifting by a width ≥ bit-width is undefined.unsigned flags = READ | WRITE; int can_read = (flags & READ) != 0; unsigned x = 1u << 31; /* ok on 32-bit unsigned */ int y = 1 << 31; /* undefined on 32-bit int (sign bit) */
int n = 5; int a = n++; /* a=5, n=6 */ int b = ++n; /* n=7, b=7 */ n += 2; int m = cond ? 1 : 0;
i = i++ is undefined. Do not write exam-style a[i++] = i++.if ((c = getchar()) != EOF) is idiomatic; if (c = 0) is a bug that compiles (assignment yields 0).for (i = 0, j = n; …).sizeof does not evaluate its operand except for VLAs. sizeof ++n does not increment n (non-VLA).(int *)p does not convert “the bytes of the object”; it reinterprets a pointer. Wrong casts → undefined.= instead of == in a condition — compiles because assignment is an expression.& when you meant && on a pointer check.i.&& / ||. Reserve bitwise ops for bits.++ inside a busy line.~ Good luck with your revision! ~