C/C++ Basics

Operators and Expressions

Operators combine values into expressions. C and C++ share almost all of them, including the integer-division rules that surprise beginners.

What is Operators and Expressions?

Operators combine values into expressions. C and C++ share almost all of them, including the integer-division rules that surprise beginners.

Use arithmetic, assignment, comparison and logical operators in C and C++.

Important points

  • Compile with warnings enabled and fix every warning.
  • Know the lifetime and type of every value.
  • Validate input and keep array bounds explicit.

C and C++ code examples

C
Run code →
main.c
#include <stdio.h>

int main(void) {
    int a = 17, b = 5;

    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);              /* integer division drops the fraction */
    printf("a %% b = %d\n", a % b);             /* remainder */
    printf("a / 5.0 = %.1f\n", a / 5.0);        /* one double operand gives a double */

    int total = 10;
    total += 5;
    total *= 2;
    printf("total = %d\n", total);

    int count = 0;
    count++;
    printf("count = %d\n", count);

    /* Comparisons and logical operators give 1 (true) or 0 (false). */
    printf("a > b: %d, a == 17: %d\n", a > b, a == 17);
    printf("a > 10 && b > 10: %d\n", a > 10 && b > 10);
    printf("a > 10 || b > 10: %d\n", a > 10 || b > 10);
    return 0;
}
Expected output
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: 1, a == 17: 1
a > 10 && b > 10: 0
a > 10 || b > 10: 1
C++
Run code →
main.cpp
#include <iostream>

int main() {
    int a = 17, b = 5;

    std::cout << "a + b = " << a + b << '\n';
    std::cout << "a - b = " << a - b << '\n';
    std::cout << "a * b = " << a * b << '\n';
    std::cout << "a / b = " << a / b << '\n';           // integer division drops the fraction
    std::cout << "a % b = " << a % b << '\n';           // remainder
    std::cout << "a / 5.0 = " << a / 5.0 << '\n';       // one double operand gives a double

    int total = 10;
    total += 5;
    total *= 2;
    std::cout << "total = " << total << '\n';

    int count = 0;
    count++;
    std::cout << "count = " << count << '\n';

    // bool values print as true/false with std::boolalpha.
    std::cout << std::boolalpha;
    std::cout << "a > b: " << (a > b) << ", a == 17: " << (a == 17) << '\n';
    std::cout << "a > 10 && b > 10: " << (a > 10 && b > 10) << '\n';
    std::cout << "a > 10 || b > 10: " << (a > 10 || b > 10) << '\n';
}
Expected output
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: true, a == 17: true
a > 10 && b > 10: false
a > 10 || b > 10: true

C and C++ comparison

Dividing two ints drops the fraction, so 17 / 5 is 3; make one operand a double to keep it. Comparisons and && / || produce 1 or 0 in C and true or false in C++, which std::boolalpha prints as words.

C

C exposes small procedural APIs and makes representation details explicit.

C++

C++ retains the low-level model while adding safer library types and abstractions.

Practice exercises

Run both versions, then modify them to observe the different language guarantees.

  • Add invalid and boundary-value inputs.
  • Compile with -Wall -Wextra -Wpedantic.
  • Move reusable declarations into a header and implementation file.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges