C++ Operators

C++ provides a wide range of operators that can be used to perform various operations on variables and values. These operators can be classified into several categories, including arithmetic, comparison, logical, and more.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. The following table lists the arithmetic operators in C++ and their corresponding symbols:

Operator Symbol Example
Addition + 5 + 3 = 8
Subtraction 5 – 3 = 2
Multiplication * 5 * 3 = 15
Division / 5 / 3 = 1
Modulus % 5 % 3 = 2
Increment ++ x++
Decrement x–

An example of using arithmetic operators in C++:

#include <iostream>
using namespace std;
int main() {
  int a = 5;
  int b = 3;
  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;
}

Assignment Operators

Assignment operators are used to assign a value to a variable. The most basic assignment operator is the equal sign (=), which assigns the value on the right side of the operator to the variable on the left side. The following table lists the assignment operators in C++ and their corresponding symbols:

Operator Symbol Example
Assignment = x =5;
Add and assignment += x += 5;
Subtract and assignment -= x -= 5;
Multiply and assignment *= x *= 5;
Divide and assignment /= x /= 5;
Modulus and assignment %= x %= 5;
#include <iostream>
using namespace std;
int main() {
    int x = 10;
    x += 5; 
    cout << "x = " << x << endl;
    x -= 5;
    cout << "x = " << x << endl;
    x *= 5;
    cout << "x = " << x << endl;
    x /= 5;
    cout << "x = " << x << endl;
    x %= 5;
    cout << "x = " << x << endl;
    return 0;
}

The output of this program will be:

x = 15
x = 10
x = 50
x = 10
x = 0

Increment and Decrement Operators

The increment operator (++) and decrement operator (–) are used to increase and decrease the value of a variable, respectively. The increment operator can be used in two ways, as a prefix or as a postfix. The difference between the two is the order in which the value is incremented and the value that is returned. The prefix operator increments the value of the variable first and then returns the new value, while the postfix operator returns the original value first and then increments it. The following table illustrates the difference between the two:

Operator Example Result
Prefix increment ++x x = x + 1
Postfix increment x++ x = x + 1
Prefix decrement –x x = x – 1
Postfix decrement x– x = x1

In the above table, the postfix increment and decrement operators (x++ and x–) first use the current value of the variable, and then increment or decrement it. On the other hand, the prefix increment and decrement operators (++x and –x) first increment or decrement the variable, and then use the new value. Here is an example of how these operators can be used:

#include <iostream>
using namespace std;
int main() {
  int x = 10;
  cout << "x = " << x << endl;
  cout << "x++ = " << x++ << endl;
  cout << "x = " << x << endl;
  cout << "++x = " << ++x << endl;
  cout << "x = " << x << endl;
  cout << "x-- = " << x-- << endl;
  cout << "x = " << x << endl;
  cout << "--x = " << --x << endl;
  cout << "x = " << x << endl;
  return 0;
}

The output of the above program will be:

x = 10
x++ = 10
x = 11
++x = 12
x = 12
x-- = 12
x = 11
--x = 10
x = 10

Relational Operators

Relational operators are used to compare two values and determine their relative relationship. The following table lists the relational operators in C++ and their corresponding symbols:

Operator Symbol Example
Equal to == x == y
Not equal to != x != y
Greater than > x > y
Less than < x < y
Greater than or equal to >= x >= y
Less than or equal to <= x <= y

In the above table, the relational operators compare the values of two variables and return a Boolean value of true or false. Here is an example of how these operators can be used:

#include <iostream>

int main()
{
int x = 5, y = 10;
std::cout << (x > y) << std::endl; // Output: 0 (false)
std::cout << (x < y) << std::endl; // Output: 1 (true)
std::cout << (x == y) << std::endl; // Output: 0 (false)
std::cout << (x != y) << std::endl; // Output: 1 (true)
std::cout << (x >= y) << std::endl; // Output: 0 (false)
std::cout << (x <= y) << std::endl; // Output: 1 (true)
return 0;
}

Logical Operators

Logical operators are used to combining two or more conditions in a statement. The following table lists the logical operators in C++ and their corresponding symbols:

Operator Symbol Example
Logical AND && x > 0 && y > 0
Logical OR || x > 0 || y > 0
Logical NOT ! !(x > 0)

In the above table, the logical operators return a Boolean value of true or false. Here is an example of how these operators can be used:

#include <iostream>
int main()
{
int x = 5, y = 10;
std::cout << (x > 0 && y > 0) << std::endl; // Output: 1 (true)
std::cout << (x > 0 || y > 0) << std::endl; // Output: 1 (true)
std::cout << !(x > 0) << std::endl; // Output: 0 (false)
return 0;
}

Bitwise Operators

Bitwise operators are used to manipulate individual bits in a variable. The following table lists the bitwise operators in C++ and their corresponding symbols:

Operator Symbol Example
Bitwise AND & x & y
Bitwise OR | x | y</
Bitwise XOR ^ x ^ y
Bitwise NOT ~ ~x
Left shift << x << y
Right shift >> x >> y

Bitwise operators perform bit-level operations on variables. The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. The bitwise OR operator (|) compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. The bitwise XOR operator (^) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. The bitwise NOT operator (~) is a unary operator that inverts all the bits of its operand. The left shift operator (​`oaicite:{“index”:0,”invalid_reason”:”Malformed citation <<) shifts the bits of the first operand to the left by the number of positions specified by the second operand. The right shift operator (>>”}`​) shifts the bits of the first operand to the right by the number of positions specified by the second operand. Here is an example of how these operators can be used:

#include <iostream>
using namespace std;
int main() {
int x = 4;
int y = 2;
cout << (x & y) << endl; // Output: 0
cout << (x | y) << endl; // Output: 6
cout << (x ^ y) << endl; // Output: 6
cout << (~x) << endl; // Output: -5
cout << (x << y) << endl; // Output: 16
cout ​2
Malformed citation << (x >>
​ y) << endl; // Output: 1
return 0;
}

Special Operators

In addition to the operators discussed above, C++ has several special operators that are used for specific purposes.

Pointer operator

The pointer operator (*) is used to declare a pointer variable, and to access the value stored at the memory location pointed to by a pointer variable. For example:

#include 
using namespace std;
int main() {
int x = 5;
int *ptr = &x;
cout << *ptr << endl;
return 0;
}

The output of this program will be:

5

Member selection operator

The member selection operator (.) is used to access the members of a class or struct. For example:

#include <iostream>
using namespace std;
class MyClass {
public:
int x;
};

int main() {
MyClass obj;
obj.x = 5;
cout << obj.x << endl;
return 0;
}

The output of this program will be:

5

Scope resolution operator

The scope resolution operator (::) is used to access a global variable or function that has been hidden by a local variable or function with the same name. It is also used to access a class member that has been hidden by a local variable or function with the same name. Here is an example of how the scope resolution operator can be used:

#include 
using namespace std;

int x = 10;  //global variable

void print() {
    int x = 20;  //local variable
    cout << x << endl;  //prints 20
    cout << ::x << endl;  //prints 10
}

int main() {
    print();
    return 0;
}

In this example, the local variable x inside the print() function hides the global variable x. To access the global variable, we use the scope resolution operator ::. The output of this program will be:

20
10

Conditional operator

The conditional operator (also called the ternary operator) is a shorthand way of writing an if-else statement. The operator takes three operands: a condition, a value to return if the condition is true, and a value to return if the condition is false. The general syntax of the operator is:

condition ? value_if_true : value_if_false

Here is an example of how the conditional operator can be used:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int y = 20;

    int max = (x > y) ? x : y;
    cout << max << endl;  //prints 20
    return 0;
}

In this example, we use the conditional operator to find the maximum of two variables. The output of this program will be:

20

sizeof operator

The sizeof operator is used to determine the number of bytes occupied by a variable or data type. The operator can be used on a variable or a data type. Here is an example of how the sizeof operator can be used:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    cout << sizeof(x) << endl;  //prints 4
    cout << sizeof(int) << endl;  //prints 4
    return 0;
}

In this example, we use the sizeof operator to find the number of bytes occupied by the variable x and the data type int. The output of this program will be:

4
4

Comma operator

The comma operator (,) is used to separate multiple expressions in a single statement. The operator evaluates each expression from left to right and returns the value of the last expression. Here is an example of how the comma operator can be used:

#include <iostream>
using namespace std;

int main() {
   int x = 5, y = 10;
   int z;

   z = (x++, y++); // x is incremented to 6, y is incremented to 11, z is assigned the value of y (11)

   cout << "x: " << x << endl; // Output: x: 6
   cout << "y: " << y << endl; // Output: y: 11
   cout << "z: " << z << endl; // Output: z: 11

   return 0;
}

In this example, the value of x is incremented to 6 and the value of y is incremented to 11. The value of z is then assigned the value of y, which is 11. The comma operator is often used in situations where multiple expressions need to be executed in a single statement, such as in a for loop or a function call.

Summary

In this article, we have covered the various types of operators that are available in C++, including arithmetic, assignment, relational, bitwise, logical, and the comma operator. We also discussed how to use these operators in different situations and provided examples of how they can be used in code. Understanding the use and behavior of these operators is an essential part of becoming a proficient C++ programmer.

This concludes the article on C++ Operators. I hope you find it informative and helpful. If you have any queries or feedback, please let me know in the comments section below.

Leave a Reply

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