Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Basic Input / Output in C++
Next article icon

Operators in C++

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.

Example:

C++
//Driver Code Starts{ #include <iostream> using namespace std; int main() {    //Driver Code Ends }  	int a = 10 + 20;        cout << a;  //Driver Code Starts{     return 0; } //Driver Code Ends } 

Output
30

Explanation: Here, ‘+‘ is an addition operator and does the addition of 10 and 20 operands and return value 30 as a result.

C++ Operator Types

C++ operators are classified into 6 types on the basis of type of operation they perform:

Table of Content

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Ternary or Conditional Operators

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic or mathematical operations on the operands. For example, ‘+‘ is used for addition.

NameSymbolDescription
Addition+Adds two operands.
Subtraction–Subtracts second operand from the first.
Multiplication*Multiplies two operands.
Division/Divides first operand by the second operand.
Modulo Operation%Returns the remainder an integer division.

Increment

++

Increase the value of operand by 1.

Decrement

—

Decrease the value of operand by 1.

Example:

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 8, b = 3;      // Addition     cout << "a + b = " << (a + b) << endl;        // Subtraction     cout << "a - b = " << (a - b) << endl;        // Multiplication     cout << "a * b = " << (a * b) << endl;        // Division     cout << "a / b = " << (a / b) << endl;        // Modulo     cout << "a % b = " << (a % b) << endl;        // Increament     cout << "++a = " << ++a << endl;        // Decrement     cout << "b-- = " << b--;  //Driver Code Starts{          return 0; } //Driver Code Ends } 

Output
a + b = 11 a - b = 5 a * b = 24 a / b = 2 a % b = 2 ++a = 9 --b = 2

Important Points:

  • The Modulo operator (%) operator should only be used with integers. Other operators can also be used with floating point values.
  • ++a and a++, both are increment operators, however, both are slightly different. In ++a, the value of the variable is incremented first and then it is used in the program. In b–, the value of the variable is assigned first and then it is incremented. Similarly happens for the decrement operator.

You may have noticed that some operator works on two operands while other work on one. On the basis of this operators are also classified as:

  • Unary: Works on single operand.
  • Binary: Works on two operands.
  • Ternary: Works on three operands.

2. Relational Operators

Relational operators are used for the comparison of the values of two operands. For example, ‘>’ check right operand is greater.

NameSymbolDescription
Is Equal To==Checks both operands are equal
Greater Than>Checks first operand is greater than the second operand
Greater Than or Equal To>=Checks first operand is greater than equal to the second operand
Less Than<Checks first operand is lesser than the second operand
Less Than or Equal To<=Checks first operand is lesser than equal to the second operand
Not Equal To!=Checks both operands are not equal

Example

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 6, b = 4;      // Equal operator     cout << "a == b is " << (a == b) << endl;        // Greater than operator     cout << "a > b is " << (a > b) << endl;        // Greater than Equal to operator     cout << "a >= b is " << (a >= b) << endl;        //  Lesser than operator     cout << "a < b is " << (a < b) << endl;        // Lesser than Equal to operator     cout << "a <= b is " << (a <= b) << endl;        // Not equal to operator     cout << "a != b is " << (a != b);  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
a == b is 0 a > b is 1 a >= b is 1 a < b is 0 a <= b is 0 a != b is 1

Note: 0 denotes false and 1 denotes true.

3. Logical Operators

Logical operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.

NameSymbolDescription
Logical AND&&Returns true only if all the operands are true or non-zero.
Logical OR||Returns true if either of the operands is true or non-zero.
Logical NOT!Returns true if the operand is false or zero.

Example:

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 6, b = 4;      // Logical AND operator     cout << "a && b is " << (a && b) << endl;        // Logical OR operator     cout << "a || b is " << (a || b) << endl;        // Logical NOT operator     cout << "!b is " << (!b);  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
a && b is 1 a || b is 1 !b is 0

4. Bitwise Operators

Bitwise operators are works on bit-level. So, compiler first converted to bit-level and then the calculation is performed on the operands.

NameSymbolDescription
Binary AND&Copies a bit to the evaluated result if it exists in both operands
Binary OR|Copies a bit to the evaluated result if it exists in any of the operand
Binary XOR^Copies the bit to the evaluated result if it is present in either of the operands but not both
Left Shift<<Shifts the value to left by the number of bits specified by the right operand.
Right Shift>>Shifts the value to right by the number of bits specified by the right operand.
One’s Complement~Changes binary digits 1 to 0 and 0 to 1

Note: Only char and int data types can be used with Bitwise Operators.

Example:

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 6, b = 4;      // Binary AND operator     cout << "a & b is " << (a & b) << endl;      // Binary OR operator     cout << "a | b is " << (a | b) << endl;      // Binary XOR operator     cout << "a ^ b is " << (a ^ b) << endl;      // Left Shift operator     cout << "a<<1 is " << (a << 1) << endl;      // Right Shift operator     cout << "a>>1 is " << (a >> 1) << endl;      // One’s Complement operator     cout << "~(a) is " << ~(a);  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
a & b is 4 a | b is 6 a ^ b is 2 a<<1 is 12 a>>1 is 3 ~(a) is -7

5. Assignment Operators

Assignment operators are used to assign value to a variable. We assign the value of right operand into left operand according to which assignment operator we use.  

Name

Symbol

Description

Assignment

=

Assigns the value on the right to the variable on the left.
Add and Assignment

+=

First add right operand value into left operand then assign that value into left operand.
Subtract and Assignment

      -=        

First subtract right operand value into left operand then assign that value into left operand.
Multiply and Assignment

*=

First multiply right operand value into left operand then assign that value into left operand.
Divide and Assignment

/=

First divide right operand value into left operand then assign that value into left operand.

Example:

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 6, b = 4;      // Assignment Operator.     cout << "a = " << a << endl;        //  Add and Assignment Operator.     cout << "a += b is " << (a += b) << endl;        // Subtract and Assignment Operator.     cout << "a -= b is " << (a -= b) << endl;        //  Multiply and Assignment Operator.     cout << "a *= b is " << (a *= b) << endl;        //  Divide and Assignment Operator.     cout << "a /= b is " << (a /= b);  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
a = 6 a += b is 10 a -= b is 6 a *= b is 24 a /= b is 6

6. Ternary or Conditional Operators

Conditional operator returns the value, based on the condition. This operator takes three operands, therefore it is known as a Ternary Operator.

Syntax:

C++
Expression1 ? Expression2 : Expression3 

In the above statement:

  • The ternary operator ? determines the answer on the basis of the evaluation of Expression1.
  • If Expression1 is true, then Expression2 gets evaluated.
  • If Expression2 is true, hen Expression3 gets evaluated.

Example:

C++
//Driver Code Starts{ #include <bits/stdc++.h> using namespace std;  int main() { //Driver Code Ends }      int a = 3, b = 4;      // Conditional Operator     int result = (a < b) ? b : a;     cout << "The greatest number "          "is " << result;  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
The greatest number is 4

Miscellaneous Operators

Apart from these operators, there are a few operators that do not fit in any of the above categories. These are:

1. sizeof Operator

sizeof operator is a unary operator used to compute the size of its operand or variable in bytes. For example,

C++
sizeof (char); sizeof (var_name); 

2. Comma Operator (,)

Comma operator is a binary operator that is used for multiple purposes. It is used as a separator or used to evaluate its first operand and discards the result; it then evaluates the second operand and returns this value (and type).

C++
int n = (m+1, m-2, m+5); int a, b, c; 

6. Addressof Operator (&)

Addressof operator is used to find the memory address in which a particular variable is stored. In C++, it is also used to create a reference.

C++
&var_name; 

5. Dot Operator(.)

Dot operator is used to access members of structure variables or class objects using their object names.

C++
obj . member; 

3. Arrow Operator

Arrow operator is used to access the variables of classes or structures through its pointer.

C++
sptr -> member; 

4. Casting Operators

Casting operators are used to convert the value of one data type to another data type. For example, for an integer value x:

C++
(float)x static_cast<float>(x) 

Operator Precedency and Associativity

When there are multiple operators in a single expression, operator precedency and associativity decide in which order and which part of expression are calculate. Precedency tells which part of expression should be calculate first and associativity tells which direction to solve when same precedency operators are in expression.

Operator Precedence

Operator precedence says which operation is calculate first in the expression when expression have different precedency operators. For example:

3 * 2 + 8;

Will be evaluated as:
(3 * 2) + 8 = 14

It is because multiplication (*) have higher precedency then addition (+).

Operator Associativity

Operator associativity says if expression have more than one operator with same precedence then calculation happen in right to left or left to right.

50 / 25 * 2 = 1

Will be evaluated as:
(50 / 25) * 2



Next Article
Basic Input / Output in C++

A

anshikajain26
Improve
Article Tags :
  • C++
  • cpp-operator
Practice Tags :
  • CPP
  • cpp-operator

Similar Reads

  • C++ Tutorial | Learn C++ Programming
    C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc. Why Learn C++?C++
    5 min read
  • Setting up C++ Development Environment
    C++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an enviro
    8 min read
  • Writing First C++ Program - Hello World Example
    The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu
    4 min read
  • C++ Variables
    In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution. Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variabl
    5 min read
  • C++ Data Types
    Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory. C++ supports a wide variety of data ty
    8 min read
  • Operators in C++
    C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language. Example: [GFGTABS] C++ //Driver Code Starts{ #include <iostream> using namespace std; int main() { //Driver Code E
    10 min read
  • Basic Input / Output in C++
    In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams. Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of byte
    5 min read
  • Decision Making in C++

    • Decision Making in C (if , if..else, Nested if, if-else-if )
      In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or n
      8 min read

    • C++ if Statement
      The C++ if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not executed based on a certain condition. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using namespace std; int
      3 min read

    • C++ if else Statement
      The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else if the condition is false. Here comes the C++ if else statement. We can use the else statement with if statement to exec
      4 min read

    • C++ if else if Ladder
      In C++, the if-else-if ladder helps the user decide from among multiple options. The C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C++ else-if ladder is bypassed. I
      3 min read

    • C++ Nested if-else Statement
      Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else statements when we want to implement multilayer conditions (condition inside the condition inside the condition and so on). C++ allows any number of nesting levels. Let's ta
      3 min read

    • Switch Statement in C++
      In C++, the switch statement is a flow control statement that is used to execute the different blocks of statements based on the value of the given expression. It is an alternative to the long if-else-if ladder which provides an easy way to execute different parts of code based on the value of the e
      6 min read

    • Jump statements in C++
      Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continue the loop inside a program or to stop the execution of a function. In C++, there is four jump statement: Table of Content continue Statementbreak Statementreturn Statementgot
      4 min read

    C++ Loops

    • C++ Loops
      In C++ programming, sometimes there is a need to perform some operation more than once or (say) n number of times. For example, suppose we want to print "Hello World" 5 times. Manually, we have to write cout for the C++ statement 5 times as shown. [GFGTABS] C++ #include <iostream> using namesp
      8 min read

    • for Loop in C++
      In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the given number of times. It is generally preferred over while and do-while loops in case the number of iterations is known beforehand. Let's take a look at an example: [GFGTABS] C++ #include <bit
      6 min read

    • C++ While Loop
      In C++, the while loop is an entry-controlled loop that repeatedly executes a block of code as long as the given condition remains true. Unlike the for loop, while loop is used in situations where we do not know the exact number of iterations of the loop beforehand as the loop execution is terminate
      3 min read

    • C++ do while Loop
      In C++, the do-while loop is an exit-controlled loop that repeatedly executes a block of code at least once and continues executing as long as a given condition remains true. Unlike the while loop, the do-while loop guarantees that the loop body will execute at least once, regardless of whether the
      4 min read

    • Range-Based for Loop in C++
      In C++, the range-based for loop introduced in C++ 11 is a version of for loop that is able to iterate over a range. This range can be anything that is iteratable, such as arrays, strings and STL containers. It provides a more readable and concise syntax compared to traditional for loops. Let's take
      3 min read

  • Functions in C++
    A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences