Operators
Operators combine operands into expressions. Precedence and associativity decide the grouping. Sequence points and the type of each operand decide whether the expression is defined at all.
Detailed Explanation
C's operators cover arithmetic (+ - * / %), comparison (== != < > <= >=), bitwise work (& | ^ ~ << >>), logical tests (&& || !), assignment (= and the compound forms), increment (++ --), indirection (*), address-of (&), subscript ([]), member access (. and ->), cast, sizeof, _Alignof, the ternary ?:, the comma operator, and function call.
Precedence is the grouping, not the order of evaluation. a + b * c groups as a + (b * c) because * binds tighter than +. Associativity breaks ties: a - b - c is (a - b) - c, and a = b = 0 is a = (b = 0) because assignment associates right. When a reader has to look up the table, write the parentheses. The cost is two characters. The saving is a defect.
Order of evaluation is a separate rule and a weaker one. For most operators the standard does not say which operand is evaluated first. f() + g() may call f first or g first. The comma operator, &&, ||, and ?: are the exceptions: they define sequence points and left-to-right evaluation. Using the same scalar object twice in one expression when at least one access is a write, without an intervening sequence point, is undefined behavior. i = i++ is the classic example.
Integer division truncates toward zero in C99 and later. The sign of a % b matches the sign of a. Shifting a negative signed value, or shifting by a count equal to the width of the type, is undefined behavior. Bitwise operators on signed types are well-defined for two's complement values in modern C, but the shift caveats remain.
Short-circuiting is a control-flow tool, not just an optimization. p != NULL && p->len > 0 will not dereference p when it is null, because && skips the right operand when the left is false. || skips the right operand when the left is true. Do not put a needed side effect only on the right-hand side unless that skip is the point.
Assignment is an expression. x = 3 has value 3 and type int. That is why if (x = 3) compiles and is almost always a mistake for if (x == 3). Compilers warn. Listen to them, or adopt the Yoda style only if your codebase already does.
Code Examples
#include <stdio.h>
int main(void)
{
int a = 7, b = 2;
printf("%d %d %d\n", a / b, a % b, a << 1); /* prints: 3 1 14 */
int *p = &a;
printf("%d\n", p != NULL && *p == 7); /* prints: 1; *p is not evaluated if p is NULL */
int x = 0;
if ((x = b) == 2) { /* assignment as expression, deliberate */
printf("x=%d\n", x); /* prints: x=2 */
}
return 0;
}#include <stdio.h>
static int twice(int *p)
{
*p += 1;
return *p;
}
int main(void)
{
int n = 1;
/* Defined: the comma operator sequences the increment, then the call. */
int r = (n++, twice(&n));
printf("%d %d\n", n, r); /* prints: 3 3 */
/* Undefined if uncommented: two modifications of n with no sequence point.
int bad = n++ + n++; */
return 0;
}Common Gotchas
`i = i++` is undefined behavior
two unsequenced modifications of `i`. Write `i++;` or `i = i + 1;`.
`if (x = 3)` assigns, then treats the result as a condition
it is true whenever the assigned value is non-zero. Enable `-Wparentheses` / `-Wmisleading-indentation`.
Shift counts must be in `0 .. width-1`
`x << 32` on a 32-bit `int` is undefined behavior, including when `x` is `0`.
Precedence does not sequence side effects
`a[i] = i++;` is undefined even though the grouping looks obvious.