C++ Operators

Operators are special symbols that tell C++ to perform an action on one or more values. Every time you add two numbers, compare a score, or update a counter, you are using an operator. This page gives you the big picture before we dive into each group in detail.

What is an operator?

An operator works on values called operands. For example, in the expression 5 + 3, the plus sign is the operator and the numbers 5 and 3 are the operands. C++ takes the operands, applies the operator, and produces a result that you can store, print, or use in another expression.

C++ organizes its operators into a few main families. You do not need to memorize them all at once, but it helps to know the categories so you can recognize what each symbol is doing when you read code.

  • Arithmetic operators, for math like addition and subtraction
  • Assignment operators, for storing values in variables
  • Comparison operators, for checking if values are equal, larger, or smaller
  • Logical operators, for combining true/false conditions

Operators in action

#include <iostream>
using namespace std;

int main() {
  int a = 10;      // assignment operator
  int b = 3;
  int sum = a + b; // arithmetic operator
  bool bigger = a > b; // comparison operator

  cout << "Sum: " << sum << "\n";
  cout << "Is a bigger than b? " << bigger << "\n";
  return 0;
}

Notice that a single line of code can use several operators at once. C++ follows rules of precedence (much like math class) to decide which operator runs first. When in doubt, use parentheses to make your intention clear.

Parentheses control the order

#include <iostream>
using namespace std;

int main() {
  int result1 = 2 + 3 * 4;   // multiplication first: 14
  int result2 = (2 + 3) * 4; // parentheses first: 20

  cout << result1 << "\n";
  cout << result2 << "\n";
  return 0;
}

Operator families at a glance

FamilyExamplePurpose
Arithmetica + bDo math with numbers
Assignmentx = 5Store a value in a variable
Comparisona == bCompare two values
Logicala && bCombine true/false results
Note: Do not confuse = (assignment, stores a value) with == (comparison, checks equality). Mixing these up is one of the most common beginner mistakes in C++.

Exercise: C++ Operators

What does the modulus operator (%) compute in C++?