C++ Operators (Operators List and Examples)

In C++, operators are used in a program to perform specific tasks or operations on data. Therefore, this article was created and published to describe the types of operators available in C++. So without any further delay, let's start.

C++ Operator Types

C++ supports the following operator types:

C++ Arithmetic Operators

The arithmetic operators are required when performing an arithmetic task in a program. The two types of arithmetic operators are as follows:

C++ Unary Operators

Unary operators are operators that only act on one operand. There are two kinds of unary operators:

Unary plus operator

The unary operator "+" comes before an operand. The unary + operator's operand (the value on which the operator operates) must be of an arithmetic or pointer type, and the result is the argument's value. In C++, here's an example of the unary plus operator in action.

if a = 5, then +a means 5
if a = 0, then +a means 0
if a = -5, then +a means -5

Unary minus operator

The unary operator "-" comes before an operand. The unary operator's operand must be of an arithmetic type, and the result is the inverse of its operand's value. The sign of the operand's value is reversed by this operator. Here's an example of how to use the unary minus operator in C++.

if a = 5, then -a means -5
if a = 0, then -a means 0 (there is no quantity known as -0)
if a = -5, then -a means 5

C++ Binary Operators

"Binary operators" are operators that operate on two operands. A binary's operand is classified as either left or right. An expression is made up of the operator and its operand. The binary operators in C++ are classified as follows.

Addition operator

The arithmetic binary operator + adds the values of its two operands, yielding the sum of their values. Here's an example of how to use the addition operator in C++.

4 + 10 results in 14
a + 5(a=2) results in 7
a + b(a=4, b=6) results in 10

Subtraction operator

The - operator takes the second operand and subtracts it from the first. Here's an example of how to use the subtraction operator in C++.

14 - 3 evaluates to 11
a - b(a=7, b=5) evaluates to 2

Multiplication operator

The x operator multiplies the operand values. Here's an example of how to use the multiplication operator in C++.

3 × 4 evaluates to 12
b × 4(b=6) evaluates to 24
a × c(a=3, c=5) evaluates to 15

Division operator

The / operator divides the first and second operands. Here's an example of how to use the division operator in C++.

100/5 evaluates to 20
a/2(a=16) evaluates to 8
a/b(a=159, b=3) evaluates to 5.3

Modulus operator

The % operator calculates the modulus of its first operand in relation to its second. That is, it yields the remainder of dividing the first operand by the second. Here's an example of how to use the modulus operator in C++.

19%6 evaluates to 1

Because 6 enters 19 three times with a remainder of one. As a result, the preceding statement evaluates to 1.

C++ Relational Operators

Relational operators are required when comparing two values in a program. The list below contains all of the relational operators available in C++.

The action of these relational operators is summarized in the table below.

1 represents true and 0 represents false.
p q p < q p <= q p = = q p > q p >= q p != q
0 1 1 1 0 0 0 1
1 0 0 0 0 1 1 1
3 3 0 1 1 0 1 0
2 6 1 1 0 0 0 1

The following are some examples of how to use relational operators:

C++ Logical Operators

When we need to compare two or more operands and want the result in boolean values (true or false), we use logical operators. C++ includes three logical operators, which are as follows:

Logical OR operator

The logical OR operator (||) joins two expressions known as operands. The logical OR ("||") operator returns true if either of its operands returns true. The following expression:

(6 <= 6) || (5 < 3)

returns 1 or true because the result of the logical OR operator returns true or 1 if one of the expressions evaluates to true.

Logical AND operator

The logical AND operators, denoted by &&, combine two expressions into one as well. Only if both of the original expressions (its operands) are true does the resulting expression have the value 1 (true). The following expression:

(6 <= 6) || (5 < 3)

returns either 0 or false. Because the expression only returns true if both expressions return true, but the expression (5 < 3) returns false in this case. As a result, the entire expression is false and returns 0, which is responsible for false.

Logical NOT operator

The logical NOT operator, denoted by the symbol "!," operates on a single expression or operand, making it a unary operator. If the expression is true, then the expression is false, and vice versa. The following expression:

!(6 <= 6)

returns 0 or false. The expression (6 <=6) is true, but on adding the logical NOT operator, the expression becomes false (return 0).

C++ Increment and Decrement Operator

When we need to add one to the operand, we use the increment operator (++), and when we need to subtract one, we use the decrement operator (—). The following statement:

a = a + 1;

is same as

++a;

or,

a++;

and

a = a - 1;

is same as

--a;

or,

a--;

The increment and decrement operators, on the other hand, come in two flavors: they can either precede or follow the operand. The prefix version comes before the operand (for example, ++a or —a), and the postfix version comes after the operand (for example, a++ or a—). The two versions have the same effect on the operand, but they behave differently when used in an expression. Let's talk about it.

Working with the prefix version

When an increment or decrement operator comes before its operand (i.e., in its prefix form), C++ performs the operation before using the operand's value. Consider the following code fragment:

int sum = 10;
int count = 5;
sum = sum + ++count;

After running the preceding code fragment, sum will be 16, not 15, and count will be 6, because, as previously stated, C++ performs the increment or decrement operation before using the operand's value in the prefix version.

Note: The prefix increment operators adhere to the change-then-use rule, which states that they first change (increment or decrement) the value of their operand before using the new value in evaluating the expression.

Working with the postfix version

When an increment or decrement follows its operand (i.e., in its postfix form), C++ evaluates the expression first using the operand's value before incrementing or decrementing the operand's value. Consider the following code fragment:

int sum = 10;
int count = 5;
sum = sum + count++;

Here, after running the above code fragment, sum will have the value of 15 and count will have the value of 6, since in the postfix version, C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value as already told.

Note: The postfix increment or decrement operator follows the use-then-change rule, which states that they first evaluate the expression using the operand's value and then change the operand's value.

C++ Conditional Operator

Conditional operators are used to store data based on a condition. This is a ternary operator, which means it requires three operands. The general form of the C++ conditional operator (?:) is shown below.

expression1 ? expression2 : expression3

If expression1 evaluates to true, i.e., 1, the value of the entire expression is expression2, otherwise, expression3 will be the value of the entire expression. As an example,

6 > 4 ? 9 : 7

evaluates to 9 because the test expression 6 > 4 is true. Whereas:

4 == 9 ? 10 : 25

evaluates to 25 because the test expression 4 == 9 is false.

C++ Bitwise Operators

The bitwise operator operates on bits and performs operations bit by bit. The bitwise operators supported by C++ are listed below.

The truth tables for &, |, and ^ are as follows:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

C++ Bitwise AND Operator Example

  A = 0011 1100 (60)
& B = 0000 1101 (13)
--------------------
      0000 1100 (12)

C++ Bitwise OR Operator Example

  A = 0011 1100 (60)
| B = 0000 1101 (13)
--------------------
      0011 1101 (61)

C++ Binary XOR Operator Example

  A = 0011 1100 (60)
^ B = 0000 1101 (13)
--------------------
      0011 0001 (49)

C++ Quiz


« Previous Tutorial Next Tutorial »