- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
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:
- Arithmetic operators
- Relational operators
- Logical operators
- Increment-decrement operators
- Conditional operators
- Bitwise operators
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:
- Unary operators work on a single operand. For example: +a
- Binary operators operate on double operands. For example: a+b
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++.
- Less than (<)
- Greater than (>)
- Less than or equal to (<=)
- Greater than or equal to (>=)
- Equal to (==)
- Not equal to (!=)
The action of these relational operators is summarized in the table below.
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:
- Check the Even or Odd Number
- Check whether the given number is a prime number or not
- Check vowel or now
- Check if the given year is a leap year
- Find the largest of two numbers
- Find the largest among three numbers
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.
- Bitwise OR operator (|): copy a bit if it exists in either operand.
- Bitwise AND operator (&): copy a bit to the result of it exists in both operands.
- Bitwise XOR operator (^): copy a bit if it is set in one operand but not both.
- Binary Ones complement operator (˜): It is a unary and has the effect of 'flipping' bits.
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)
« Previous Tutorial Next Tutorial »