C Mastery / Operators: Arithmetic, Comparison, Logical
Part 1 — The Core Language

Operators: Arithmetic, Comparison, Logical

This chapter covers arithmetic, comparison, and logical operators: their semantics, their results, and their common traps. These are the operators you use constantly, so they are concise but precise.

Why This Matters

These operators look like their counterparts in other languages, but C's integer arithmetic has overflow rules and its logical operators have specific "truthiness" and short-circuit semantics that differ from languages with real booleans.

Prerequisites

Core Concept

Arithmetic operators

OperatorMeaningNotes
+additionsigned overflow is UB; unsigned wraps
-subtractionsame overflow rules
*multiplicationsame overflow rules
/divisiondivision by zero is UB
%remainder% is not defined for negative operands in C89; defined since C99

For / and %: if both operands are non-negative, the result is non-negative. Since C99, the result of / truncates toward zero, and a % b has the sign of a. Division by zero, and INT_MIN / -1, are undefined behavior.

Comparison operators

OperatorMeaning
==equal
!=not equal
<less than
>greater than
<=less than or equal
>=greater than or equal

Comparison operators yield 1 (true) or 0 (false), of type int. They follow the usual arithmetic conversions (c.types.uac), so comparing signed and unsigned values has specific, often surprising rules.

Logical operators

OperatorMeaning
!logical NOT
&&logical AND
``logical OR

Logical operators treat any nonzero value as true and zero as false. !x yields 1 if x is zero, else 0. && and || yield 1 or 0, and they short-circuit: the right operand is evaluated only if needed.

Syntax

a + b;
a - b;
a * b;
a / b;
a % b;

a == b; a != b; a < b; a > b; a <= b; a >= b;

!a;
a && b;
a || b;

Examples

Arithmetic

#include <stdio.h>

int main(void)
{
    printf("%d\n", 7 / 2);    /* 3: truncates toward zero */
    printf("%d\n", -7 / 2);   /* -3 (C99 and later) */
    printf("%d\n", 7 % 2);    /* 1 */
    printf("%d\n", -7 % 2);   /* -1 (C99 and later: sign of dividend) */
    return 0;
}

Expected output (C99 and later):

3
-3
1
-1

Short-circuit evaluation

#include <stdio.h>

int is_positive(int x) { printf("checking %d\n", x); return x > 0; }

int main(void)
{
    int a = 0;
    int b = 5;
    if (a != 0 && is_positive(b)) {
        /* not executed because a != 0 is false */
    }
    /* is_positive(b) was never called */
    return 0;
}

Because a != 0 is false, is_positive(b) is not evaluated.

How It Works

Arithmetic and comparison operators first apply the usual arithmetic conversions (c.types.uac) to bring both operands to a common type, then compute the result in that type. Logical operators evaluate to int with value 0 or 1.

Variations

Logical vs. bitwise

Do not confuse &&/|| with &/|:

a && b;   /* logical AND: 0 or 1, short-circuits */
a & b;    /* bitwise AND: bit-by-bit, no short-circuit */

This distinction is a frequent source of bugs (covered in c.core.10).

Common Mistakes

-Wparentheses or similar.)

Undefined Behavior

to follow the dividend's sign. STANDARD-VERSION-DEPENDENT

Portability

(must avoid).

concern only for very old or non-conforming compilers.

Under the Hood

Arithmetic maps to CPU integer instructions. Comparison maps to compare instructions that set condition flags, which branch instructions then test. Logical operators compile to conditional branches (for short-circuiting) or to compare-and-set sequences.

Practical Usage

arithmetic where wrapping is desired).

behavior.

Exercises

1. Write a program that prints 7/2, -7/2, 7%2, and -7%2 and verify the C99 behavior. 2. Demonstrate short-circuiting with a function that prints when called. 3. Write a safe addition function that returns an error on signed overflow without invoking UB.

Deep Challenge

Explain why INT_MIN / -1 is undefined behavior but INT_MIN % -1 is also undefined, while 0 / 1 and 0 % 1 are fine. Relate your answer to the representability of the result.

References

(relational/equality), §6.5.13–14 (logical).

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.ops.arith05
c.ops.compare05
c.ops.logical05