codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

10 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Operators
⚡C++ Programming

Operators

Updated 2026-05-12
30 min read

Operators

In C++, operators are symbols that perform operations on variables and values. They are essential for performing calculations, comparisons, and logical evaluations in your programs. Understanding operators is crucial as they form the backbone of any programming language.

Introduction

Operators in C++ can be broadly categorized into several types: arithmetic, assignment, comparison, logical, bitwise, ternary, sizeof, comma, and scope resolution. Each type serves a specific purpose and has its own set of rules and precedence levels. In this tutorial, we will explore each type of operator with examples to help you understand their usage and importance.

Core Content

Arithmetic Operators

Arithmetic operators are used for performing basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
arithmetic.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 3;
6 cout << "Addition: " << a + b << endl;
7 cout << "Subtraction: " << a - b << endl;
8 cout << "Multiplication: " << a * b << endl;
9 cout << "Division: " << a / b << endl;
10 cout << "Modulus: " << a % b << endl;
11 return 0;
12}
Output
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator is =.

OperatorDescriptionEquivalent To
=Simple assignmenta = b
+=Addition assignmenta = a + b
-=Subtraction assignmenta = a - b
*=Multiplication assignmenta = a * b
/=Division assignmenta = a / b
%=Modulus assignmenta = a % b
assignment.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 5;
6 a += b; // Equivalent to: a = a + b
7 cout << "a after += : " << a << endl;
8
9 a -= b; // Equivalent to: a = a - b
10 cout << "a after -= : " << a << endl;
11
12 a *= b; // Equivalent to: a = a * b
13 cout << "a after *= : " << a << endl;
14
15 a /= b; // Equivalent to: a = a / b
16 cout << "a after /= : " << a << endl;
17
18 a %= b; // Equivalent to: a = a % b
19 cout << "a after %= : " << a << endl;
20
21 return 0;
22}
Output
a after += : 15
a after -= : 10
a after *= : 50
a after /= : 10
a after %= : 0

Comparison Operators

Comparison operators are used to compare two values. They return a boolean result (true or false).

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
comparison.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 5;
6
7 if (a == b) cout << "a is equal to b" << endl;
8 else cout << "a is not equal to b" << endl;
9
10 if (a > b) cout << "a is greater than b" << endl;
11 else cout << "a is not greater than b" << endl;
12
13 return 0;
14}
Output
a is not equal to b
a is greater than b

Logical Operators

Logical operators are used to combine conditional statements. They include AND, OR, and NOT operations.

OperatorDescriptionExample
&&Logical ANDa && b
||Logical ORa || b
!Logical NOT!a
logical.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 5;
6
7 if (a > 0 && b > 0) cout << "Both a and b are positive" << endl;
8 else cout << "At least one of a or b is non-positive" << endl;
9
10 if (a > 0 || b > 0) cout << "At least one of a or b is positive" << endl;
11 else cout << "Neither a nor b is positive" << endl;
12
13 if (!a) cout << "a is zero" << endl;
14 else cout << "a is not zero" << endl;
15
16 return 0;
17}
Output
Both a and b are positive
At least one of a or b is positive
a is not zero

Bitwise Operators

Bitwise operators perform operations on individual bits of numbers. They include AND, OR, XOR, NOT, left shift, and right shift.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 1
>>Right shifta >> 1
bitwise.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 5, b = 3; // Binary: a = 0101, b = 0011
6
7 cout << "Bitwise AND: " << (a & b) << endl; // 0001 -> 1
8 cout << "Bitwise OR: " << (a | b) << endl; // 0111 -> 7
9 cout << "Bitwise XOR: " << (a ^ b) << endl; // 0110 -> 6
10 cout << "Bitwise NOT of a: " << (~a) << endl; // 1010 -> -6 (two's complement)
11
12 cout << "Left shift of a by 1: " << (a << 1) << endl; // 1010 -> 10
13 cout << "Right shift of a by 1: " << (a >> 1) << endl; // 0010 -> 2
14
15 return 0;
16}
Output
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT of a: -6
Left shift of a by 1: 10
Right shift of a by 1: 2

Ternary Operator

The ternary operator is a shorthand for the if-else statement. It takes three operands.

SyntaxDescription
condition ? expression1 : expression2If condition is true, evaluate expression1; otherwise, evaluate expression2
ternary.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 5;
6
7 int max = (a > b) ? a : b;
8 cout << "Max value: " << max << endl;
9
10 return 0;
11}
Output
Max value: 10

sizeof Operator

The sizeof operator returns the size, in bytes, of its operand.

SyntaxDescription
sizeof(type)Returns the size of the specified type
sizeof.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 cout << "Size of int: " << sizeof(int) << " bytes" << endl;
6 cout << "Size of char: " << sizeof(char) << " byte" << endl;
7 cout << "Size of double: " << sizeof(double) << " bytes" << endl;
8
9 return 0;
10}
Output
Size of int: 4 bytes
Size of char: 1 byte
Size of double: 8 bytes

Comma Operator

The comma operator evaluates its first operand and discards the result, then evaluates and returns the second operand.

SyntaxDescription
expression1, expression2Evaluates both expressions; returns the value of expression2
comma.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 5, b;
6
7 b = (a++, a + 3); // First, a is incremented to 6, then a + 3 is evaluated as 9
8 cout << "b: " << b << endl; // Output: 9
9
10 return 0;
11}
Output
b: 9

Scope Resolution Operator

The scope resolution operator :: is used to access members of a namespace or class.

SyntaxDescription
namespace::identifierAccesses identifier in the specified namespace
scope_resolution.cpp
1#include <iostream>
2using namespace std;
3
4namespace MyNamespace {
5 int value = 10;
6}
7
8int main() {
9 cout << "Value from namespace: " << MyNamespace::value << endl;
10
11 return 0;
12}
Output
Value from namespace: 10

Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.

PrecedenceOperator(s)
Highest()
[]
->
::
++, -- (postfix)
!, ~, +, - (unary)
*, /, %
+, -
<<, >>
<, <=, >, >=
==, !=
&
|
^
&&
||
?:
= (and other assignment operators)
Lowest,
precedence.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 5, c = 2;
6
7 int result = a + b * c; // Multiplication has higher precedence than addition
8 cout << "Result: " << result << endl; // Output: 20
9
10 return 0;
11}
Output
Result: 20

Practical Example

Let's create a simple program that calculates the area of a rectangle using user input and demonstrates the use of various operators.

rectangle_area.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 double length, width;
6
7 cout << "Enter the length of the rectangle: ";
8 cin >> length;
9
10 cout << "Enter the width of the rectangle: ";
11 cin >> width;
12
13 double area = length * width;
14 cout << "The area of the rectangle is: " << area << endl;
15
16 return 0;
17}

Summary

Operator TypeDescription
ArithmeticBasic math operations
AssignmentAssign values to variables
ComparisonCompare two values
LogicalCombine conditional statements
BitwisePerform bitwise operations
TernaryShorthand for if-else
sizeofGet size of data types
CommaEvaluate multiple expressions
Scope ResolutionAccess namespace or class members

What's Next?

In the next tutorial, we will explore Booleans and how they are used in C++ to represent true/false conditions. Understanding booleans is essential for controlling program flow with conditional statements.

Stay tuned!


PreviousType Conversion & Casting OperatorsNext Booleans

Recommended Gear

Type Conversion & Casting OperatorsBooleans