0Pricing
C++ Academy · Lesson

Basic Operators and Type Conversion

Master arithmetic, comparison, and logical operators plus implicit and explicit type conversions.

Arithmetic Operators

The standard arithmetic operators: + - * / and modulo %. Modulo works only on integers.

int a = 17, b = 5;
std::cout << a + b << "\n";  // 22
std::cout << a - b << "\n";  // 12
std::cout << a * b << "\n";  // 85
std::cout << a / b << "\n";  // 3 (integer division)
std::cout << a % b << "\n";  // 2

Integer vs Floating Division

When both operands are integers, division truncates toward zero. Cast one operand to a floating type for a real division.

int a = 7, b = 2;
std::cout << a / b << "\n";              // 3
std::cout << static_cast<double>(a) / b; // 3.5

All lessons in this course

  1. Setting Up a C++ Compiler
  2. Hello World and Build Process
  3. Variables, Auto Type Inference and constexpr
  4. Basic Operators and Type Conversion
← Back to C++ Academy