Operators in C++

Operators in C++ are symbols and keywords that tell the compiler to perform specific operations on values or variables. They are used everywhere in programming, from basic arithmetic and comparisons to logical decisions, memory-related expressions, and object-oriented behavior. If variables store data, operators are the tools that let a program actually work with that data.

A simple statement like total = price + tax; already uses two operators. The assignment operator = stores a value, and the arithmetic operator + adds two operands together. In this article, we will understand what operators in C++ are, learn the major categories, study examples, see the difference between unary, binary, and ternary operators, and review common mistakes that beginners make while using them.

What Are Operators in C++?

An operator in C++ is a symbol or operator-like language token that performs an action on one or more operands. Operands are the values, variables, or expressions on which the operator works.

ExpressionOperatorOperandsMeaning
a + b+a, bAdds two values
x = 10=x, 10Assigns a value
count++++countIncrements by one
a > b>a, bChecks whether one value is greater

Operators define what action should happen in an expression, while operands provide the data on which that action happens.

Why Operators Matter in C++

  • They perform calculations and comparisons.
  • They control assignment and state changes.
  • They help build conditions for decision-making.
  • They support loops, bit-level logic, and pointer-related work.
  • They are central to nearly every non-trivial expression in C++.

Without operators, you could declare variables, but you could not combine values, compare results, update counters, or write meaningful conditions. That is why operators appear in almost every program from the first lesson onward.

Types of Operators in C++

C++ has many operators, but beginners can learn them more easily by grouping them into categories.

CategoryExamplesMain Use
Arithmetic operators+, -, *, /, %Perform mathematical calculations
Relational operators==, !=, >, <, >=, <=Compare values
Logical operators&&, ||, !Combine or negate conditions
Assignment operators=, +=, -=, *=, /=Store or update values
Increment and decrement operators++, --Increase or decrease by one
Bitwise operators&, |, ^, ~, <<, >>Operate on individual bits
Conditional operator?:Choose between two expressions
Special and miscellaneous operatorssizeof, ,, ., ->, [], ()Support size checks, member access, function calls, indexing, and more

Arithmetic Operators in C++

Arithmetic operators are used for numeric calculations.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remaindera % b
int a = 10;
int b = 3;

std::cout << a + b << std::endl;
std::cout << a - b << std::endl;
std::cout << a * b << std::endl;
std::cout << a / b << std::endl;
std::cout << a % b << std::endl;

One important beginner point is that integer division removes the fractional part. In the example above, 10 / 3 gives 3, not 3.333..., because both operands are integers.

Relational Operators in C++

Relational operators compare two values and produce a Boolean result, either true or false.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
int marks = 75;
std::cout << (marks >= 40) << std::endl;
std::cout << (marks == 100) << std::endl;

These operators are used heavily in if statements, loops, and conditions throughout real programs.

Logical Operators in C++

Logical operators work with Boolean expressions.

OperatorMeaningExample
&&Logical ANDBoth conditions must be true
||Logical ORAt least one condition must be true
!Logical NOTReverses a Boolean value
int age = 20;
bool hasId = true;

if (age >= 18 && hasId)
{
    std::cout << "Allowed" << std::endl;
}

Logical operators are common in conditions that depend on multiple checks at the same time.

Assignment Operators in C++

Assignment operators store or update a value in a variable.

OperatorMeaningEquivalent Form
=Assigna = b
+=Add and assigna = a + b
-=Subtract and assigna = a - b
*=Multiply and assigna = a * b
/=Divide and assigna = a / b
%=Remainder and assigna = a % b

Compound assignment operators are useful because they express updates compactly and clearly.

Increment and Decrement Operators in C++

The increment operator ++ increases a value by one, and the decrement operator -- decreases a value by one. These are very common in loops and counters.

OperatorMeaning
++Increase by one
--Decrease by one

They can appear in prefix form or postfix form.

int x = 5;
int a = ++x;   // x becomes 6, then a gets 6
int b = x--;   // b gets 6, then x becomes 5

The difference between prefix and postfix matters in larger expressions, so it is better to use them carefully and clearly rather than pack too much behavior into one line.

Bitwise Operators in C++

Bitwise operators work directly on the binary representation of integers. They are especially useful in embedded programming, low-level optimization, flags, registers, and masking operations.

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

These operators are important enough that they often deserve separate articles later, but at this stage you should at least know what they are and why they exist.

Conditional Operator in C++

The conditional operator ?: is the only common ternary operator in C++. It works with three parts: a condition, an expression for the true case, and an expression for the false case.

int age = 20;
std::string result = (age >= 18) ? "Adult" : "Minor";

This operator is useful for short conditional expressions, but if the logic becomes complicated, a normal if-else block is often easier to read.

Unary, Binary, and Ternary Operators in C++

Operators can also be classified by the number of operands they work on.

TypeOperands RequiredExample
Unary operatorOne operand++x, !flag, -value
Binary operatorTwo operandsa + b, x == y, m && n
Ternary operatorThree operandscondition ? x : y

This classification is useful because it helps you reason about how an expression is evaluated and what data each operator needs.

Operator Precedence and Associativity in C++

When an expression contains multiple operators, C++ uses precedence and associativity rules to decide evaluation order. Multiplication usually has higher precedence than addition, and assignment is evaluated later than many arithmetic operations.

int result = 2 + 3 * 4;

Here, multiplication happens first, so the result becomes 14, not 20. Parentheses can always be used to make the intention explicit and improve readability.

For beginners, the safest rule is simple: if there is any chance of confusion, use parentheses. Clear code matters more than proving you remember a precedence table from memory.

Special Operators You Will Meet Early in C++

Not every useful operator fits cleanly into arithmetic or logical categories. C++ also includes several operators that support ordinary programming structure. For example, sizeof tells you the size of a type or object, [] is used for array or container indexing, () is used for function calls and grouping, . accesses members of an object, and -> accesses members through a pointer. These operators appear constantly in practical code even if beginners first notice them only one by one.

OperatorCommon UseExample
sizeofGets size in bytessizeof(int)
[]Indexes an array or similar structurescores[0]
()Calls a function or groups expressionsprintData()
.Accesses object membersstudent.name
->Accesses members through a pointerptr->value

Writing Clear Operator Expressions

As expressions become longer, the technical correctness of the operators is only part of the problem. Readability also matters. A line that mixes assignments, increments, comparisons, and arithmetic all at once may still compile, but it becomes difficult to review and easy to misread. Good C++ style usually favors breaking complex expressions into smaller steps when the logic is important.

For example, instead of packing several updates into one statement, it is often clearer to calculate an intermediate result, store it in a named variable, and then continue. This keeps operator use understandable and reduces bugs caused by hidden evaluation details. Strong operator knowledge is useful, but strong expression design is what keeps that knowledge safe in real programs.

Common Mistakes with Operators in C++

MistakeProblemBetter Approach
Using = instead of ==Assignment happens instead of comparisonUse == inside comparisons
Forgetting integer division rulesFractional part is lostUse floating-point operands when decimals are needed
Overusing prefix and postfix in complex expressionsCode becomes hard to reason aboutUse increments in simple statements
Ignoring precedenceExpression may not evaluate as expectedAdd parentheses for clarity
Mixing logical and bitwise operators carelesslyResults differ in meaning and behaviorUse logical operators for conditions and bitwise operators for bit manipulation

Best Practices for Using Operators in C++

  • Prefer clarity over clever expression writing.
  • Use parentheses when operator precedence might confuse the reader.
  • Do not hide too many state changes inside one expression.
  • Use logical operators for Boolean conditions, not bitwise substitutes.
  • Be careful with assignment inside conditions unless the intent is absolutely clear.

FAQs

What is the difference between = and == in C++?

= assigns a value to a variable, while == checks whether two values are equal.

Why does 10 / 3 give 3 in C++?

Because both operands are integers, so C++ performs integer division and discards the fractional part.

What is the ternary operator in C++?

The ternary operator is ?:. It chooses between two expressions based on a condition.

Are bitwise operators and logical operators the same?

No. Logical operators work with Boolean conditions, while bitwise operators work on individual bits of integer values.

Why should I use parentheses even if I know precedence rules?

Because parentheses make intent clearer for readers and reduce mistakes in complex expressions.


Continue learning C++ in order
Follow the topic sequence with the previous and next lesson.