Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Operators Explained: Types, Examples, and Sample Programs

What Are Operators?

In C++, an operator is a symbol that performs an operation on one or more operands. Operators can be arithmetic, logical, relational, bitwise, or assignment, among others.

Example:

a = x + y;

Here, x and y are operands, and + is the addition operator. The compiler adds x and y and stores the result in a.

In this tutorial you’ll learn:

Types of Operators in C++

C++ defines six main categories of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Other Operators

Arithmetic Operators

These operators perform mathematical calculations.

OperatorDescription
+Adds two operands.
-Subtracts the second operand from the first.
*Multiplies two operands.
/Divides the numerator by the denominator.
%Returns the remainder of a division.
++Increments an integer by one.
--Decrements an integer by one.

Example program:

#include <iostream>
using namespace std;
int main() {
    int a = 11;
    int b = 5;
    int c;

    cout << "a + b is :" << a+b << endl;
    cout << "a - b is :" << a-b << endl;
    cout << "a * b is :" << a*b << endl;
    cout << "a / b is :" << a/b << endl;
    cout << "a % b is :" << a%b << endl;
    cout << "a++ is :" << a++ << endl;
    cout << "a-- is :" << a-- << endl;
    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Code explanation is omitted for brevity.

Relational Operators

These operators compare operands and return a boolean result.

OperatorDescription
==Checks equality.
!=Checks inequality.
>Greater than.
<Less than.
>=Greater than or equal to.
<=Less than or equal to.

Example program:

#include <iostream>
using namespace std;

int main() {
    int a = 11;
    int b = 5;

    cout << "a=11, b=5" << endl;
    if (a == b) {
        cout << "a == b is true" << endl;
    } else {
        cout << "a == b is false" << endl;
    }
    if (a < b) {
        cout << "a < b is true" << endl;
    } else {
        cout << "a < b is false" << endl;
    }
    if (a > b) {
        cout << "a > b is true" << endl;
    } else {
        cout << "a > b is false" << endl;
    }
    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Logical Operators

Logical operators combine boolean expressions.

OperatorDescription
&&Logical AND – true if both operands are non‑zero.
||Logical OR – true if at least one operand is non‑zero.
!Logical NOT – inverts the boolean value.

Example program:

#include <iostream>
using namespace std;
int main() {
    int a = 5, b = 2, c = 6, d = 4;
    if (a == b && c > d)
        cout << "a equals to b AND c is greater than d\n";
    else
        cout << "AND operation returned false\n";

    if (a == b || c > d)
        cout << "a equals to b OR c is greater than d\n";
    else
        cout << "Neither a is equal to b nor c is greater than d\n";

    if (!b)
        cout << "b is zero\n";
    else
        cout << "b is not zero";

    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Bitwise Operators

Bitwise operators perform operations on individual bits of integer values.

OperatorDescription
&Bitwise AND – 1 only if both bits are 1.
|Bitwise OR – 1 if at least one bit is 1.
^Bitwise XOR – 1 if bits differ.
<<Left shift – shifts bits left, filling with 0.
>>Right shift – shifts bits right, discarding lower bits.
~Bitwise NOT – inverts all bits.

Example program:

#include <iostream>
using namespace std;
int main() {
    unsigned int p = 60;  // 0011 1100
    unsigned int q = 13;  // 0000 1101
    int z = 0;

    z = p & q;
    cout << "p&q is : " << z << endl; // 12

    z = p | q;
    cout << "p|q is : " << z << endl; // 61

    z = p ^ q;
    cout << "p^q is : " << z << endl; // 49

    z = ~p;
    cout << "~p is : " << z << endl; // -61

    z = p << 2;
    cout << "p<<2 is: " << z << endl; // 240

    z = p >> 2;
    cout << "p>>2 is : " << z << endl; // 15

    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Assignment Operators

These operators assign values to variables, often combining an arithmetic operation.

OperatorDescription
=Simple assignment.
+=Adds and assigns.
-=Subtracts and assigns.
*=Multiplies and assigns.
/=Divides and assigns.

Example program:

#include <iostream>
using namespace std;
int main() {
    int x = 5;
    cout << "Initial value of x is " << x << "\n";

    x += 5;
    cout << "x += 5 gives :" << x << "\n";

    x -= 5;
    cout << "x -= 5 gives : " << x << "\n";

    x *= 5;
    cout << "x *= 5 gives :" << x << "\n";

    x /= 5;
    cout << "x /= 5 gives : " << x << "\n";

    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Other Operators

sizeof Operator

Returns the size, in bytes, of a type or object.

Example:

#include <iostream>
using namespace std;
int main() {
    cout << "Size of int : " << sizeof(int) << "\n";
    cout << "Size of char : " << sizeof(char) << "\n";
    cout << "Size of float : " << sizeof(float) << "\n";
    cout << "Size of double : " << sizeof(double) << "\n";
    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Comma Operator

Executes a sequence of expressions, returning the value of the last.

Example:

#include <iostream>
using namespace std;
int main() {
    int x, y;
    y = 100;
    x = (y++, y + 10, 99 + y);
    cout << x;
    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Conditional Operator

Also known as the ternary operator, it evaluates a condition and returns one of two expressions.

Syntax: condition ? expr1 : expr2

Example:

#include <iostream>
using namespace std;
int main() {
    int a = 1, b;
    b = (a < 10) ? 2 : 5;
    cout << "value of b: " << b << endl;
    return 0;
}

Output:

C++ Operators Explained: Types, Examples, and Sample Programs

Operator Precedence

When multiple operators appear in a single expression, precedence rules determine the order of evaluation. From highest to lowest precedence:

(), [], *, /, %, +/-, <<, >>, ==, !=, ^, |, &&, ||, ?:, =, +=, -=, *=, /=

Summary

C Language

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. C++ Operator Overloading – A Practical Guide with Code Examples
  3. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  4. C++ Structs Explained with a Practical Example
  5. C++ Classes & Objects: A Practical Guide with Code Examples
  6. C++ Polymorphism Explained: Practical Examples & Key Concepts
  7. Mastering std::list in C++: Syntax, Functions & Practical Examples
  8. C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
  9. Understanding C# Interfaces: Definition, Example, and Practical Use
  10. Java Variables and Data Types – A Comprehensive Guide with Examples