Back to C++ Home

Operators & Expressions

Operators are the fundamental building blocks of C++ expressions. They allow you to perform calculations, make comparisons, manipulate bits, and control program flow. Understanding operators deeply is crucial because they appear in virtually every line of C++ code. From simple arithmetic to complex bit manipulations, operators enable you to express your logic concisely and efficiently.

Types of Operators

C++ provides various types of operators to perform different operations on operands. Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C++ has a rich set of operators that can be categorized into several groups, each serving specific purposes in program development.

Understanding operator types helps you choose the right tool for each task. Some operators are used constantly (like arithmetic and assignment), while others are specialized (like bitwise operators for low-level programming). Mastery of operators is essential for writing efficient, readable, and correct C++ code.

1. Arithmetic Operators

Used to perform mathematical operations:

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
++Incrementa++ or ++a
--Decrementa-- or --a
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    
    cout << "a + b = " << (a + b) << endl;  // 13
    cout << "a - b = " << (a - b) << endl;  // 7
    cout << "a * b = " << (a * b) << endl;  // 30
    cout << "a / b = " << (a / b) << endl;  // 3
    cout << "a % b = " << (a % b) << endl;  // 1
    
    int x = 5;
    cout << "x++ = " << x++ << endl;  // 5 (post-increment)
    cout << "x = " << x << endl;      // 6
    
    int y = 5;
    cout << "++y = " << ++y << endl;  // 6 (pre-increment)
    cout << "y = " << y << endl;      // 6
    
    return 0;
}

2. Relational Operators

Used to compare two values. Returns true (1) or false (0):

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

3. Logical Operators

Used to combine multiple conditions:

OperatorDescriptionExample
&&Logical AND(a > 5) && (b < 10)
||Logical OR(a > 5) || (b < 10)
!Logical NOT!(a > 5)

4. Assignment Operators

Used to assign values to variables:

OperatorDescriptionEquivalent To
=Assignmenta = b
+=Add and assigna += b (a = a + b)
-=Subtract and assigna -= b (a = a - b)
*=Multiply and assigna *= b (a = a * b)
/=Divide and assigna /= b (a = a / b)
%=Modulus and assigna %= b (a = a % b)

5. Bitwise Operators

Used to perform operations on individual bits:

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 2
>>Right shifta >> 2

6. Other Operators

  • sizeof: Returns the size of a variable or data type
  • ?: (Ternary): Conditional operator (condition ? value1 : value2)
  • :: (Scope resolution): Accesses members of a namespace or class
  • .* and ->*: Pointer to member operators
  • new: Dynamic memory allocation
  • delete: Dynamic memory deallocation

Operator Precedence

Operators are evaluated in a specific order. Higher precedence operators are evaluated first:

  1. Postfix operators (++, --)
  2. Unary operators (+, -, !, ~, sizeof)
  3. Multiplicative (*, /, %)
  4. Additive (+, -)
  5. Shift (<<, >>)
  6. Relational (<, <=, >, >=)
  7. Equality (==, !=)
  8. Bitwise AND (&)
  9. Bitwise XOR (^)
  10. Bitwise OR (|)
  11. Logical AND (&&)
  12. Logical OR (||)
  13. Conditional (?:)
  14. Assignment (=, +=, -=, etc.)

Use parentheses () to override precedence and make expressions clearer. When in doubt, use parentheses to ensure the intended order of evaluation and improve code readability.

Summary

TopicKey PointsDifficulty
Arithmetic Operators+, -, *, /, %, ++, -- for mathematical operationsBeginner
Relational Operators==, !=, >, <, >=, <= for comparisons, return boolBeginner
Logical Operators&&, ||, ! for combining conditions, short-circuit evaluationBeginner
Assignment Operators=, +=, -=, *=, /=, %= for combined assignment and operationBeginner
Bitwise Operators&, |, ^, ~, <<, >> for bit-level manipulationIntermediate
Operator PrecedenceOrder of evaluation, use parentheses to overrideIntermediate

Frequently Asked Questions

Q1: What is the difference between ++i and i++?

++i (pre-increment) increments i first, then returns the new value.i++ (post-increment) returns the current value first, then increments i. For standalone statements, both are equivalent, but in expressions: int x = ++i;assigns incremented value, while int x = i++; assigns original value.

Q2: What is short-circuit evaluation in logical operators?

Short-circuit evaluation means the second operand is not evaluated if the result can be determined from the first. Fora && b, if a is false, b is not evaluated. Fora || b, if a is true, b is not evaluated. This is useful for avoiding errors (e.g., checking pointer before dereferencing).

Q3: What is the difference between = and == operators?

= is the assignment operator (assigns value), while== is the equality operator (compares values). Using = instead of == in conditions is a common error that assigns instead of comparing, often causing bugs. Modern compilers warn about this.

Q4: When should I use bitwise operators?

Bitwise operators are used for low-level programming: setting/clearing flags, efficient multiplication/division by powers of 2 (shift), cryptography, graphics programming, embedded systems, and optimizing certain algorithms. They operate on individual bits rather than entire values.

Q5: What is operator associativity?

Associativity determines the order of evaluation when operators have the same precedence. Left-associative operators (most) evaluate left to right: a - b - c = (a - b) - c. Right-associative operators (assignment, ternary) evaluate right to left: a = b = c= a = (b = c).

Q6: Can I overload operators in C++?

Yes, C++ allows operator overloading for user-defined types (classes). You can define custom behavior for operators like +, -, *, /, ==, <<, etc. This enables intuitive syntax like vector1 + vector2. Some operators cannot be overloaded (::, ., .*, ?:), and you cannot change operator precedence or number of operands.

Q7: What happens when I divide by zero in C++?

Division by zero causes undefined behavior for integers (may crash or produce garbage). For floating-point numbers, division by zero may produce infinity (inf) or NaN (Not a Number). Always check for zero before division to prevent errors and ensure program stability.

Q8: What is the ternary operator and when should I use it?

The ternary operator ? : is a conditional operator:condition ? value_if_true : value_if_false. Use it for simple conditional assignments when it improves readability, but avoid nested ternaries as they reduce clarity. It's more concise than if-else for simple cases.

Conclusion

Operators are fundamental to C++ programming, enabling you to perform calculations, make decisions, and manipulate data efficiently. Understanding the different types of operators, their precedence, and associativity is crucial for writing correct and efficient code.

While most operators are straightforward, some require careful attention: the difference between = and ==, pre-increment vs post-increment, and operator precedence can cause subtle bugs. Always use parentheses when the order of evaluation is unclear, and be mindful of short-circuit evaluation in logical expressions.

As you advance, you'll learn to leverage operator overloading to create intuitive interfaces for your classes, and use bitwise operators for performance-critical code. Mastery of operators forms the foundation for understanding more complex C++ features and writing professional-quality code.

Related Links

🔹 Author: Dr. J. Siva Ramakrishna

🔹 Institution: Narayana Engineering College, Gudur

🔹 Last Updated: 9 January 2026