How to use operators

Calculate arithmetic

The basic arithmetic operators are +, -, *, /, and %. Integer division discards the fractional part.

int quotient = 7 / 2;       // 3
double exact = 7.0 / 2.0;  // 3.5
int remainder = 7 % 2;     // 1

Compare values

Comparisons produce zero or one. Do not confuse equality == with assignment =.

if (age >= 18 && has_ticket) {
    printf("Allowed\n");
}

Respect precedence

Multiplication and division bind before addition and subtraction. Parentheses make mixed expressions easier to verify.

double average = (first + second + third) / 3.0;

Avoid unsafe arithmetic

Signed integer overflow is undefined behavior, division by zero is invalid, and unsigned arithmetic wraps. Check limits before calculations involving untrusted or very large values.

  • Use limits.h constants such as INT_MAX
  • Check a divisor before division
  • Do not use unsigned types merely to hide negative-input problems