C++ Operators

What are Operators in C++?

Operators are symbols used to perform operations on variables and values in a C++ program.

Operators help in performing calculations, comparing values, and making logical decisions.

Examples of C++ operators include +, -, *, /, %, ==, >, <, &&, and ||.

Why Operators are Important?

Operators are important in C++ because they help programmers:

• Perform mathematical calculations.

• Compare two or more values.

• Combine multiple conditions.

• Assign and update variable values.

• Perform bitwise operations.

Operators make C++ programs more powerful and help control program logic.

Types of Operators in C++

C provides different types of operators for different purposes.

The main types of operators are:

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Assignment Operators

• Increment and Decrement Operators

• Bitwise Operators

• Conditional (Ternary) Operator

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus on numeric values.

Operator

Name

Description

+

Addition

Adds two values

-

Subtraction

Subtracts one value from another

*

Multiplication

Multiplies two values

/

Division

Divides one value by another

%

Modulus

Returns the remainder of division

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 5;

cout << “Addition: ” << a + b << endl;
cout << “Subtraction: ” << a – b << endl;
cout << “Multiplication: ” << a * b << endl;
cout << “Division: ” << a / b << endl;
cout << “Modulus: ” << a % b << endl;

return 0;
}

Output

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0

Explanation

a + b adds two values.

a - b subtracts the second value from the first.

a * b multiplies two values.

a / b divides the first value by the second.

a % b returns the remainder of division.

cout displays the results on the screen.

Relational Operators in C++

Relational operators are used to compare two values in a C++ program.

They return true (1) if the condition is satisfied and false (0) if it is not.

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 20;

cout << “a == b: ” << (a == b) << endl;
cout << “a != b: ” << (a != b) << endl;
cout << “a > b: ” << (a > b) << endl;
cout << “a < b: ” << (a < b) << endl;
cout << “a >= b: ” << (a >= b) << endl;
cout << “a <= b: ” << (a <= b) << endl;

return 0;
}

Output

a == b: 0
a != b: 1
a > b: 0
a < b: 1
a >= b: 0
a <= b: 1

Explanation

== checks whether two values are equal.

!= checks whether two values are not equal.

> checks whether the first value is greater.

< checks whether the first value is smaller.

>= checks whether the first value is greater than or equal.

<= checks whether the first value is smaller than or equal.

C++ displays 1 for true and 0 for false.

Logical Operators in C++

Logical operators are used to combine two or more conditions in a C++ program. They return true (1) when the condition is satisfied and false (0) otherwise.

  • && – Logical AND

  • || – Logical OR

  • ! – Logical NOT

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 20;

cout << “Logical AND: ” << (a < 15 && b > 15) << endl;
cout << “Logical OR: ” << (a > 15 || b > 15) << endl;
cout << “Logical NOT: ” << !(a < 15) << endl;

return 0;
}

Output

Logical AND: 1
Logical OR: 1
Logical NOT: 0

Explanation

&& (AND): Returns true when both conditions are true.

|| (OR): Returns true when at least one condition is true.

! (NOT): Reverses the result of a condition.

1 represents true and 0 represents false.

Assignment Operators in C++

Assignment operators are used to assign values to variables in a C++ program. They can also perform arithmetic operations and assign the updated result to a variable.

Operator

Example

Meaning

=

a = 10

Assigns value

+=

a += 5

a = a + 5

-=

a -= 5

a = a - 5

*=

a *= 5

a = a * 5

/=

a /= 5

a = a / 5

%=

a %= 5

a = a % 5

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10;

cout << “Initial value: ” << a << endl;

a += 5;
cout << “After += : ” << a << endl;

a -= 3;
cout << “After -= : ” << a << endl;

a *= 2;
cout << “After *= : ” << a << endl;

a /= 4;
cout << “After /= : ” << a << endl;

a %= 3;
cout << “After %= : ” << a << endl;

return 0;
}

Output

Initial value: 10
After += : 15
After -= : 12
After *= : 24
After /= : 6
After %= : 0

Explanation

a = 10 assigns the initial value.

a += 5 adds 5 to a.

a -= 3 subtracts 3 from a.

a *= 2 multiplies a by 2.

a /= 4 divides a by 4.

a %= 3 stores the remainder in a.

Increment and Decrement Operators in C++.

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

  • ++ – Increment operator increases the value by 1.

  • -- – Decrement operator decreases the value by 1.

C++ supports two forms:

  • Pre-increment (++a): Increases the value before using it.

  • Post-increment (a++): Uses the current value before increasing it.

Similarly, pre-decrement (--a) and post-decrement (a--) decrease the value.

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10;

cout << “Initial value: ” << a << endl;
cout << “Pre-increment: ” << ++a << endl;
cout << “Post-increment: ” << a++ << endl;
cout << “After increment: ” << a << endl;
cout << “Pre-decrement: ” << –a << endl;
cout << “Post-decrement: ” << a– << endl;
cout << “After decrement: ” << a << endl;

return 0;
}

Output

Initial value: 10
Pre-increment: 11
Post-increment: 11
After increment: 12
Pre-decrement: 11
Post-decrement: 11
After decrement: 10

Explanation

++a increases the value before displaying it.

a++ displays the current value, then increases it.

--a decreases the value before displaying it.

a-- displays the current value, then decreases it.

Bitwise Operators in C++

Bitwise operators are used to perform operations on individual bits of integer values. They work directly with the binary representation of numbers.

  • & – Bitwise AND

  • | – Bitwise OR

  • ^ – Bitwise XOR

  • ~ – Bitwise NOT

  • << – Left Shift

  • >> – Right Shift

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 5, b = 3;

cout << “Bitwise AND: ” << (a & b) << endl;
cout << “Bitwise OR: ” << (a | b) << endl;
cout << “Bitwise XOR: ” << (a ^ b) << endl;
cout << “Bitwise NOT: ” << (~a) << endl;
cout << “Left Shift: ” << (a << 1) << endl;
cout << “Right Shift: ” << (a >> 1) << endl;

return 0;
}

Output

Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left Shift: 10
Right Shift: 2

Explanation

& performs bitwise AND operation.

| performs bitwise OR operation.

^ performs bitwise XOR operation.

~ reverses all bits.

<< shifts bits to the left.

>> shifts bits to the right.

Conditional (Ternary) Operator in C++.

The conditional operator is also known as the ternary operator because it uses three operands. It is a shorthand alternative to the if-else statement.

Syntax:

condition ? expression1 : expression2;

  • If the condition is true, expression1 is executed.

  • If the condition is false, expression2 is executed.

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 20;

int largest = (a > b) ? a : b;

cout << “Largest number: ” << largest;

return 0;
}

Output

Largest number: 20

Explanation

a and b store two integer values.

(a > b) checks whether a is greater than b.

? selects the value if the condition is true.

: separates the true and false expressions.

If the condition is false, b is selected.

The result is stored in largest.

Special Operators in C++.

C++ provides some special operators that are used for specific programming tasks, such as accessing memory addresses, accessing members of objects, and managing memory.

Common special operators include:

  • sizeof – Returns the size of a data type or variable in bytes.

  • & – Returns the memory address of a variable.

  • * – Used to declare and access pointer variables.

  • . – Accesses members of an object.

  • -> – Accesses members through a pointer to an object.

  • :: – Scope resolution operator, used to access global or class members.

Example Program

Program

#include <iostream>
using namespace std;

int a = 10; // Global variable

class Student {
public:
int age = 20;
};

int main() {
int *ptr = &a;
Student s;
Student *p = &s;

cout << “Size of a: ” << sizeof(a) << ” bytes” << endl;
cout << “Address of a: ” << &a << endl;
cout << “Value using pointer: ” << *ptr << endl;
cout << “Age using dot operator: ” << s.age << endl;
cout << “Age using arrow operator: ” << p->age << endl;
cout << “Scope resolution: ” << ::a << endl;

return 0;
}

Output

Size of a: 4 bytes
Address of a: (Memory address)
Value using pointer: 10
Age using dot operator: 20
Age using arrow operator: 20
Scope resolution: 10

Explanation

sizeof(a) returns the size of variable a in bytes.

&a returns the memory address of variable a.

*ptr accesses the value stored at the memory address.

s.age accesses the class member using the dot operator.

p->age accesses the class member through an object pointer.

::a accesses the global variable using the scope resolution operator.

Key Points

  • Operators are symbols used to perform operations on variables and values.

  • Arithmetic operators perform mathematical calculations.

  • Relational operators compare two values.

  • Logical operators combine multiple conditions.

  • Assignment operators assign values to variables.

  • Increment and decrement operators increase or decrease a value by 1.

  • Bitwise operators perform operations on individual bits.

  • The conditional (ternary) operator is a shorthand for if-else.

  • Special operators perform tasks such as accessing memory addresses and class members.