Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Basic Operators

What you’ll learn: arithmetic, relational and logical operators, integer division, the modulus operator and operator precedence.

Before you start: Lessons 1-3.

In programming, an OPERATOR is a special symbol that performs an operation on one or more values or variables (called OPERANDS). For example, in 5 + 3, the + is the operator and 5 and 3 are the operands.

We will explore three main categories of operators in this lesson:

  1. Arithmetic Operators: For performing mathematical calculations.
  2. Relational Operators: For comparing two values.
  3. Logical Operators: For combining multiple comparisons.

Arithmetic Operators

The lesson works with two integers:

int a = 10;
int b = 3;

+ (addition), - (subtraction) and * (multiplication) work just as they do in math: a + b is 13, a - b is 7 and a * b is 30.

/ (Division)

This one is tricky! When you divide two integers in C, the result is also an integer. The fractional part is simply thrown away (truncated). This is called INTEGER DIVISION. So a / b is 3, not 3.333.

To get a precise decimal result, at least one of the operands must be a floating-point type (like double). We can achieve this by “casting” one of the integers to a double:

double precise_division = (double)a / b;

Now precise_division holds 3.333333.

% (Modulus)

This operator gives you the REMAINDER of an integer division. a % b is 1, because 10 divided by 3 is 3 with 1 left over. It is incredibly useful: for example, n % 2 is 0 exactly when n is even. (To print a % sign with printf, write %%, because a single % starts a format specifier.)

Relational Operators

RELATIONAL OPERATORS are used to compare two values. The result of a relational operation is a “truth” value, and in C that value is an int:

  • 0 represents FALSE.
  • Any non-zero integer represents TRUE. Comparisons always give 1.

The six relational operators are == (equal to; note the TWO equals signs), != (not equal to), >, <, >= and <=. With a as 10 and b as 3, a == 10 is 1 and a == b is 0.

(Modern C does have a boolean type, bool, with the values true and false. You will meet it in a later lesson. Even so, comparisons and logical operators still produce the int values 1 and 0.)

Logical Operators

LOGICAL OPERATORS are used to combine or modify logical (true/false) expressions:

  • && (AND) is true only if BOTH sides are true.
  • || (OR) is true if AT LEAST ONE side is true.
  • ! (NOT) flips a value: !0 is 1, and !1 is 0.
int is_player_alive = 1; // 1 for true
int has_key = 0;         // 0 for false

Here is_player_alive && has_key is 0 (the player cannot open the door), is_player_alive || has_key is 1, and !has_key is 1.

Operator Precedence

Just like in math (PEMDAS/BODMAS), operators in C have a default order of evaluation called OPERATOR PRECEDENCE. For example, * and / are evaluated before + and -, so 5 + 2 * 10 is 25, not 70.

You can use parentheses () to force a specific order of evaluation, just like in math: (5 + 2) * 10 is 70. It’s often a good idea to use parentheses to make your code clearer, even when not strictly necessary.

Full Source

/**
 * @file 4_basic_operators.c
 * @brief Part 1, Lesson 4: Basic Operators
 * @author dunamismax
 * @date 06-15-2025
 *
 * This lesson covers the fundamental operators in C used for performing
 * calculations and making comparisons.
 */

/*
 * =====================================================================================
 * |                                   - LESSON START -                                  |
 * =====================================================================================
 *
 * In programming, an OPERATOR is a special symbol that performs an operation on
 * one or more values or variables (called OPERANDS). For example, in `5 + 3`,
 * the `+` is the operator and `5` and `3` are the operands.
 *
 * We will explore three main categories of operators in this lesson:
 * 1. Arithmetic Operators: For performing mathematical calculations.
 * 2. Relational Operators: For comparing two values.
 * 3. Logical Operators: For combining multiple comparisons.
 */

#include <stdio.h>

int main(void)
{
    // Let's declare some variables to work with.
    int a = 10;
    int b = 3;

    // --- Part 1: Arithmetic Operators ---

    printf("--- Part 1: Arithmetic Operators ---\n");
    printf("Initial values: a = %d, b = %d\n\n", a, b);

    // `+` (Addition)
    printf("a + b = %d\n", a + b);

    // `-` (Subtraction)
    printf("a - b = %d\n", a - b);

    // `*` (Multiplication)
    printf("a * b = %d\n", a * b);

    /*
     * `/` (Division) - This one is tricky!
     * When you divide two integers in C, the result is also an integer.
     * The fractional part is simply thrown away (truncated). This is called
     * INTEGER DIVISION.
     */
    printf("Integer Division (a / b) = %d\n", a / b); // 10 / 3 is 3, not 3.333

    /*
     * To get a precise decimal result, at least one of the operands must be a
     * floating-point type (like `double`). We can achieve this by "casting"
     * one of the integers to a double.
     */
    double precise_division = (double)a / b;
    printf("Floating-Point Division ((double)a / b) = %f\n", precise_division);

    /*
     * `%` (Modulus)
     * This operator gives you the REMAINDER of an integer division. It is
     * incredibly useful in many programming scenarios.
     */
    printf("Modulus (a %% b) = %d\n", a % b); // 10 divided by 3 is 3 with a remainder of 1.
    // Note: To print a literal '%' character in printf, you must type '%%'.
    printf("\n");

    // --- Part 2: Relational Operators ---

    /*
     * RELATIONAL OPERATORS are used to compare two values. The result of a
     * relational operation is a "truth" value, and in C that value is an `int`:
     * - `0` represents FALSE.
     * - Any non-zero integer represents TRUE. Comparisons always give `1`.
     *
     * (Modern C does have a boolean type, `bool`, with the values `true` and
     * `false`. You will meet it in a later lesson. Even so, comparisons and
     * logical operators still produce the `int` values 1 and 0.)
     */
    printf("--- Part 2: Relational Operators ---\n");
    printf("The results below will be 1 for TRUE and 0 for FALSE.\n\n");

    // `==` (Equal to) - VERY IMPORTANT:
    // A single `=` is for assignment. A double `==` is for comparison.
    // This is one of the most common beginner mistakes!
    printf("Is a == 10? %d\n", a == 10); // True (1)
    printf("Is a == b?  %d\n", a == b);  // False (0)

    // `!=` (Not equal to)
    printf("Is a != b?  %d\n", a != b); // True (1)

    // `>` (Greater than)
    printf("Is a > b?   %d\n", a > b); // True (1)

    // `<` (Less than)
    printf("Is a < b?   %d\n", a < b); // False (0)

    // `>=` (Greater than or equal to)
    printf("Is a >= 10? %d\n", a >= 10); // True (1)

    // `<=` (Less than or equal to)
    printf("Is b <= 3?  %d\n", b <= 3); // True (1)
    printf("\n");

    // --- Part 3: Logical Operators ---

    /*
     * LOGICAL OPERATORS are used to combine or modify logical (true/false) expressions.
     */
    printf("--- Part 3: Logical Operators ---\n");
    int is_player_alive = 1; // 1 for true
    int has_key = 0;         // 0 for false
    printf("Player is alive: %d, Player has key: %d\n\n", is_player_alive, has_key);

    // `&&` (Logical AND) - evaluates to true ONLY if BOTH operands are true.
    printf("Can open door (is_player_alive && has_key)? %d\n", is_player_alive && has_key);

    // `||` (Logical OR) - evaluates to true if AT LEAST ONE operand is true.
    printf("Is player alive OR has key? %d\n", is_player_alive || has_key);

    // `!` (Logical NOT) - inverts the truth value of its operand.
    printf("The opposite of has_key (!has_key) is: %d\n", !has_key);
    printf("\n");

    // --- Part 4: Operator Precedence ---
    printf("--- Part 4: Operator Precedence ---\n");
    /*
     * Just like in math (PEMDAS/BODMAS), operators in C have a default order
     * of evaluation called OPERATOR PRECEDENCE. For example, `*` and `/` are
     * evaluated before `+` and `-`.
     */
    int result1 = 5 + 2 * 10;             // 2*10 is done first, then 5 is added.
    printf("5 + 2 * 10 = %d\n", result1); // Prints 25, not 70.

    /*
     * You can use parentheses `()` to force a specific order of evaluation,
     * just like in math. It's often a good idea to use parentheses to make
     * your code clearer, even when not strictly necessary.
     */
    int result2 = (5 + 2) * 10;             // (5+2) is done first.
    printf("(5 + 2) * 10 = %d\n", result2); // Prints 70.

    return 0;
}

/*
 * =====================================================================================
 * |                                    - LESSON END -                                   |
 * =====================================================================================
 *
 * In this lesson, you learned about C's fundamental operators:
 * - Arithmetic (`+`, `-`, `*`, `/`, `%`) for calculations.
 * - Relational (`==`, `!=`, `>`, `<`, `>=`, `<=`) for comparisons.
 * - Logical (`&&`, `||`, `!`) for combining true/false conditions.
 *
 * You also learned about integer division, the modulus operator, and how to
 * use parentheses `()` to control the order of operations.
 *
 * HOW TO COMPILE AND RUN THIS CODE:
 *
 * 1. Open a terminal or command prompt.
 * 2. Navigate to the directory where you saved this file.
 * 3. Use the C compiler, `cc`, to create an executable file:
 *    `cc -Wall -Wextra -std=c17 -o 4_basic_operators 4_basic_operators.c`
 * 4. Run the executable:
 *    - On Linux/macOS:   `./4_basic_operators`
 *    - On Windows (WSL): `./4_basic_operators`
 */

How to Compile and Run

cc -Wall -Wextra -std=c17 -o 4_basic_operators 4_basic_operators.c
./4_basic_operators

Try It Yourself

Practise what this lesson taught. Change a copy of the lesson file, or start a new one.

  1. Read two integers with scanf and print their sum, difference, product, integer quotient and remainder.
  2. Print 7 / 2, 7.0 / 2 and (double)7 / 2. Explain each result.
  3. Write an expression that is 1 when an integer year is a leap year (divisible by 4, and not by 100 unless also by 400), and test it on 1900, 2000 and 2024.

Try each one yourself before you look at a worked solution. The solutions, with the reasoning in their comments, are in exercises/04-basic-operators/ in the repository and on this lesson’s page on the website.