Types of Operators in C++

Operators are fundamental elements in any programming language, and C++ is no exception. Operators are special symbols that tell the compiler to perform specific mathematical, logical, or relational operations. They help programmers manipulate data and variables in a program. C++ supports a rich set of operators that make it a powerful and flexible language for problem-solving.

In this detailed post, we will explore the main types of operators in C++, their syntax, working, and examples. Understanding these operators is essential for mastering C++ programming because they form the foundation for writing efficient and logical code.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and modulus. These are the most basic and widely used operators in programming.

List of Arithmetic Operators

OperatorDescriptionExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Explanation

  1. Addition (+)
    The addition operator adds two operands.
    Example:
    int result = a + b;
    If a = 10 and b = 5, then result = 15.
  2. Subtraction (-)
    This operator subtracts the second operand from the first.
    Example:
    int result = a - b;
    If a = 10 and b = 5, then result = 5.
  3. Multiplication (*)
    Multiplies two numbers.
    Example:
    int result = a * b;
    If a = 10 and b = 5, then result = 50.
  4. Division (/)
    Divides the first operand by the second.
    Example:
    int result = a / b;
    If a = 10 and b = 5, then result = 2. Note: If both operands are integers, the division result will also be an integer. To get a floating-point result, at least one operand must be a float or double.
  5. Modulus (%)
    Returns the remainder after division.
    Example:
    int result = a % b;
    If a = 10 and b = 3, then result = 1.

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout &lt;&lt; "Addition: " &lt;&lt; a + b &lt;&lt; endl;
cout &lt;&lt; "Subtraction: " &lt;&lt; a - b &lt;&lt; endl;
cout &lt;&lt; "Multiplication: " &lt;&lt; a * b &lt;&lt; endl;
cout &lt;&lt; "Division: " &lt;&lt; a / b &lt;&lt; endl;
cout &lt;&lt; "Modulus: " &lt;&lt; a % b &lt;&lt; endl;
return 0;
}

2. Relational Operators

Relational operators are used to compare two values or expressions. The result of a relational operation is always a boolean value — either true (1) or false (0). These operators help in decision-making and conditional statements like if, while, and for.

List of Relational Operators

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

Explanation

  1. Equal to (==)
    Checks if two operands are equal.
    Example: a == b returns true if both are equal.
  2. Not equal to (!=)
    Returns true if operands are not equal.
    Example: a != b
  3. Greater than (>)
    Returns true if the left operand is greater than the right.
    Example: a > b
  4. Less than (<)
    Returns true if the left operand is smaller than the right.
    Example: a < b
  5. Greater than or equal to (>=)
    Returns true if the left operand is greater than or equal to the right.
    Example: a >= b
  6. Less than or equal to (<=)
    Returns true if the left operand is less than or equal to the right.
    Example: a <= b

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout &lt;&lt; (a == b) &lt;&lt; endl;
cout &lt;&lt; (a != b) &lt;&lt; endl;
cout &lt;&lt; (a &gt; b) &lt;&lt; endl;
cout &lt;&lt; (a &lt; b) &lt;&lt; endl;
cout &lt;&lt; (a &gt;= b) &lt;&lt; endl;
cout &lt;&lt; (a &lt;= b) &lt;&lt; endl;
return 0;
}

3. Logical Operators

Logical operators are used to perform logical operations between two or more expressions. These are mainly used in decision-making and conditional statements.

List of Logical Operators

OperatorDescriptionExample
&&Logical ANDa && b
!Logical NOT!a

Explanation

  1. Logical AND (&&)
    Returns true if both operands are true.
    Example:
    If a = true and b = true, then a && b is true.
  2. Logical OR (||)
    Returns true if at least one operand is true.
    Example:
    If a = true and b = false, then a || b is true.
  3. Logical NOT (!)
    Reverses the logical state of its operand.
    Example:
    If a = true, then !a is false.

Example Program

#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
cout &lt;&lt; (a &amp;&amp; b) &lt;&lt; endl;
cout &lt;&lt; (a || b) &lt;&lt; endl;
cout &lt;&lt; (!a) &lt;&lt; endl;
return 0;
}

4. Assignment Operators

Assignment operators are used to assign values to variables. The most basic assignment operator is =, but C++ also supports compound assignment operators that combine arithmetic operations with assignment.

List of Assignment Operators

OperatorDescriptionExample
=Assigns valuea = b
+=Adds and assignsa += b (a = a + b)
-=Subtracts and assignsa -= b (a = a – b)
*=Multiplies and assignsa *= b (a = a * b)
/=Divides and assignsa /= b (a = a / b)
%=Modulus and assignsa %= b (a = a % b)

Explanation

  1. Simple Assignment (=)
    Assigns the right-hand value to the left-hand variable.
    Example: a = 5;
  2. Add and Assign (+=)
    Adds and assigns the result.
    Example: a += 5; is equivalent to a = a + 5;
  3. Subtract and Assign (-=)
    Subtracts and assigns the result.
    Example: a -= 3;
  4. Multiply and Assign (*=)
    Multiplies and assigns the result.
    Example: a *= 2;
  5. Divide and Assign (/=)
    Divides and assigns the result.
    Example: a /= 4;
  6. Modulus and Assign (%=)
    Computes remainder and assigns.
    Example: a %= 3;

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 10;
a += 5;
cout &lt;&lt; a &lt;&lt; endl;
a -= 3;
cout &lt;&lt; a &lt;&lt; endl;
a *= 2;
cout &lt;&lt; a &lt;&lt; endl;
a /= 4;
cout &lt;&lt; a &lt;&lt; endl;
a %= 3;
cout &lt;&lt; a &lt;&lt; endl;
return 0;
}

5. Bitwise Operators

Bitwise operators operate on bits and perform bit-by-bit operations. These operators are used in low-level programming, such as system programming and embedded systems.

List of Bitwise Operators

OperatorDescriptionExample
&Bitwise ANDa & b
Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left Shifta << b
>>Right Shifta >> b

Explanation

  1. Bitwise AND (&)
    Performs AND operation on each bit.
    Example:
    a = 5 (0101)
    b = 3 (0011)
    a & b = 1 (0001)
  2. Bitwise OR (|)
    Performs OR operation on each bit.
    a | b = 7 (0111)
  3. Bitwise XOR (^)
    Performs exclusive OR.
    a ^ b = 6 (0110)
  4. Bitwise NOT (~)
    Flips each bit.
    Example:
    If a = 5 (0101), then ~a = -6.
  5. Left Shift (<<)
    Shifts bits to the left, filling zeros.
    Example: a << 1 shifts bits one place left.
  6. Right Shift (>>)
    Shifts bits to the right.
    Example: a >> 1

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout &lt;&lt; (a &amp; b) &lt;&lt; endl;
cout &lt;&lt; (a | b) &lt;&lt; endl;
cout &lt;&lt; (a ^ b) &lt;&lt; endl;
cout &lt;&lt; (~a) &lt;&lt; endl;
cout &lt;&lt; (a &lt;&lt; 1) &lt;&lt; endl;
cout &lt;&lt; (a &gt;&gt; 1) &lt;&lt; endl;
return 0;
}

6. Increment and Decrement Operators

These operators are used to increase or decrease the value of a variable by one. They are shorthand notations for adding or subtracting 1.

Types of Increment and Decrement Operators

OperatorDescription
++Increment by 1
Decrement by 1

Explanation

  1. Pre-increment (++a)
    Increments the value before it is used in an expression.
    Example:
    If a = 5, then b = ++a results in a = 6 and b = 6.
  2. Post-increment (a++)
    Increments the value after it is used.
    Example:
    If a = 5, then b = a++ results in a = 6 and b = 5.
  3. Pre-decrement (–a)
    Decreases the value before use.
    Example:
    If a = 5, then b = --a results in a = 4 and b = 4.
  4. Post-decrement (a–)
    Decreases the value after use.
    Example:
    If a = 5, then b = a-- results in a = 4 and b = 5.

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 5;
cout &lt;&lt; ++a &lt;&lt; endl;
cout &lt;&lt; a++ &lt;&lt; endl;
cout &lt;&lt; --a &lt;&lt; endl;
cout &lt;&lt; a-- &lt;&lt; endl;
return 0;
}

7. Miscellaneous Operators

C++ provides some additional operators that do not fall into the categories above. These are very useful in special cases.

List of Miscellaneous Operators

  1. sizeof Operator
    Returns the size (in bytes) of a variable or data type.
    Example:
    sizeof(int) or sizeof(a)
  2. Conditional or Ternary Operator (?:)
    A shorthand for if-else statement.
    Syntax:
    condition ? expression1 : expression2
    Example:
    int result = (a > b) ? a : b;
  3. Comma Operator (,)
    Allows multiple expressions to be separated. The last expression’s value is returned.
    Example:
    int a = (b = 3, b + 2);
  4. Pointer Operators ( and &)*
    * is used to declare or dereference a pointer.
    & gives the address of a variable.
    Example:
    int *ptr = &a;
  5. Type Casting Operator
    Converts one data type into another.
    Example:
    (float)a converts integer a to float.
  6. Scope Resolution Operator (::)
    Used to access global variables or class members.
    Example:
    ::x refers to the global variable x if a local variable with the same name exists.
  7. Member Access Operator (.) and (->)
    . is used to access members of a structure or class object.
    -> is used with pointers to access class or structure members.
    Example:
    obj.member or ptr->member
  8. new and delete Operators
    Used for dynamic memory allocation and deallocation.
    Example:
    int *ptr = new int;
    delete ptr;

Example Program

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
int result = (a &gt; b) ? a : b;
cout &lt;&lt; "Larger value: " &lt;&lt; result &lt;&lt; endl;
cout &lt;&lt; "Size of int: " &lt;&lt; sizeof(int) &lt;&lt; endl;
int *ptr = &amp;a;
cout &lt;&lt; "Address of a: " &lt;&lt; ptr &lt;&lt; endl;
cout &lt;&lt; "Value using pointer: " &lt;&lt; *ptr &lt;&lt; endl;
return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *