Loading...
Start Coding

C++ Operators

Master C++ with Coddy

Learn C++ through interactive, bite-sized lessons. Master memory management, OOP, and build powerful applications.

Start C++ Journey →

Operators in C++ are symbols that perform specific operations on one or more operands. They are essential for manipulating data and performing calculations in your programs.

Types of Operators

C++ provides various types of operators, each serving a unique purpose:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric operands.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)

Example:

int a = 10, b = 3;
int sum = a + b;  // 13
int difference = a - b;  // 7
int product = a * b;  // 30
int quotient = a / b;  // 3
int remainder = a % b;  // 1

Relational Operators

Relational operators compare two values and return a boolean result.

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example:

int x = 5, y = 10;
bool isEqual = (x == y);  // false
bool isGreater = (x > y);  // false
bool isLessOrEqual = (x <= y);  // true

Logical Operators

Logical operators perform boolean operations on operands.

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Example:

bool a = true, b = false;
bool result1 = a && b;  // false
bool result2 = a || b;  // true
bool result3 = !a;  // false

Bitwise Operators

Bitwise operators perform operations on individual bits of integer operands.

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

Example:

int a = 5, b = 3;
int result1 = a & b;  // 1
int result2 = a | b;  // 7
int result3 = a ^ b;  // 6
int result4 = ~a;  // -6
int result5 = a << 1;  // 10
int result6 = a >> 1;  // 2

Assignment Operators

Assignment operators assign values to variables.

Operator Description
= Simple assignment
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign

Example:

int x = 10;
x += 5;  // x is now 15
x -= 3;  // x is now 12
x *= 2;  // x is now 24
x /= 4;  // x is now 6
x %= 4;  // x is now 2

Increment and Decrement Operators

These operators increase or decrease the value of a variable by 1.

Operator Description
++ Increment
-- Decrement

Example:

int a = 5;
++a;  // a is now 6 (pre-increment)
a++;  // a is now 7 (post-increment)
--a;  // a is now 6 (pre-decrement)
a--;  // a is now 5 (post-decrement)

Operator Precedence

Operators in C++ have different precedence levels, determining the order of evaluation in complex expressions. It's crucial to understand these precedence rules to avoid unexpected results in your code.

For a comprehensive guide on operator precedence, refer to the C++ Syntax section.

Best Practices

  • Use parentheses to clarify complex expressions and override default precedence.
  • Be cautious with increment and decrement operators in complex expressions.
  • Avoid overusing the comma operator, as it can make code less readable.
  • When working with bitwise operators, consider using C++ Constants for better code clarity.

Understanding C++ operators is fundamental to writing efficient and correct code. They form the building blocks of expressions and are essential in C++ Function Basics and more advanced topics like C++ Classes and Objects.