An operator is a symbol that performs a specific mathematical or logical operation on operands.
Example: Addition :
2 + 3, where 2 and 3 are the operands and + is the operator.
We can say that operators are the foundation of any programming language, the program is incomplete without the operators as we use them in all of our programs.
Following are the types of Operators in C++.
- Arithmetic Operators
- Increment and Decrement Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Other Operators
C++ Arithmetic Operators
These are used to perform mathematical operations on operands such as addition, subtraction, multiplication, division etc.
adds two operands. eg, a+b.
subtracts two operands. eg, a-b.
multiplies two operands. eg, a*b.
divides the operand by the second. eg, a/b.
returns the remainder when the first operand is divided by the second. For example, a%b.
Click Here for example of Arithmetic operators.
C++ Increment and Decrement Operators
These are the Unary Operators as they operate on a single operand. They are
-- operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.
- Post-Increment or Post-Decrement:
First, the value is used for operation and then incremented or decremented. Represented as a++ or a–.
- Pre-Increment Pre-Decrement:
Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.
Click Here for example of Increment and Decrement operators.
C++ Assignment Operators
Assignment operators are used in a program to assign a value of the right-hand side to the left-hand side as shown below.
//10 is assign to the variable a
int a = 10;
//the value of b (10) is assigned to a
int b = 20;
int a = b;
These are the short version formed by combining the two operators. For example Instead of writing,
int a = a+5, we can write
a += 5.
Following are the list of assignment operators used in C++.
|=||Simple assignment operator, Assigns values from right side operands to left side operand.|
|+=||Add AND assignment operator, It adds the right operand to the left operand and assigns the result to the left operand.|
|-=||Subtract AND assignment operator, It subtracts the right operand from the left operand and assigns the result to the left operand.|
|*=||Multiply AND assignment operator, It multiplies the right operand with the left operand and assigns the result to the left operand.|
|/=||Divide AND assignment operator, It divides left operand with the right operand and assigns the result to the left operand.|
|%=||Modulus AND assignment operator, It takes modulus using two operands and assigns the result to the left operand.|
|<<=||Left shift AND assignment operator.|
|>>=||Right shift AND assignment operator.|
|&=||Bitwise AND assignment operator.|
|^=||Bitwise exclusive OR and assignment operator.|
||=||Bitwise inclusive OR and assignment operator.|
Click Here for example of Assignment Operators operators.
C++ Relational Operators
These operators are used to compare two values and return Boolean results. For example: it checks if the operand is equal to another operand or not or an operand is greater than the other operand or not or checks less than between two values, etc.
List of relational operators in C++:
|>=||Greater than or equal to.|
|<=||Less than or equal to.|
Click Here for example of Relational Operators operators.
C++ Logical Operators
Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. They are used to combine two different expressions together.
The following are the C++ Logical Operators. Assume X holds the value 1 and Y holds 0
|&& (logical and)||If both the operands are non-zero, then the condition becomes true.|
||| (logical or)||If any of the two operands are non-zero, then the condition becomes true.|
|! (logical not)||Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.|
Click Here for example of Logical Operators operators.
C++ Bitwise Operators
Bitwise operators are used to perform a bit-level operation on operands. They are used in testing, setting, or shifting the actual bits in a program.
You can see the truth table below.
|p||q||p & q||p | q||p ^ q||~p|
Below are the list of Bitwise operators:
|Operators||Name of operators|
Click Here for example of Bitwise Operators operators.
Apart from the above operators, there are various other operators used in C++ programming. They are listed below.
|It returns the size of a variable, constant, array, etc. |
|The ternary operator (or Conditional operator) returns a true or false after checking the condition. It works like if..else statement.|
|Comma Operator, this operator is used to link related operators together. The value of the entire comma expression is the value of the last expression of the comma-separated list.|
|This is a pointer operator, used to point the address of a variable, or we can say that it represents the memory address of the operand to which it points.|
|This is also a pointer variable, Indirection Operator. It returns the value of the variable that it points to.|
|dot operator, this operator is used to access the members of the class, structure and objects.|
|This operator is usually seen used with pointers to access the class or struct variables.|
ptr->score = 90;
Operators Precedence in C++
In a program, there may be an expression that uses more than one operator, in such case operator precedence determines which is to be evaluated first and next.
Let us take an example:
int result = 10 + 5 * 10;
The output of the above expression is 60 because the precedence of multiplication is higher than the addition. So first (5 * 10) is evaluated and then 10 is added to the result.
The following shows the precedence and associativity of C++ operators:
Associativity refers to the direction of the operators to be evaluated first and next. It may be right to left or left to right.
|Postfix||()  -> . ++ – –||Left to right|
|Unary||+ – ! ~ ++ – – (type)* & sizeof||Right to left|
|Multiplicative||* / %||Left to right|
|Additive||+ –||Right to left|
|Shift||<< >>||Left to right|
|Relational||< <= > >=||Left to right|
|Equality||== !=/td>||Right to left|
|Bitwise AND||&||Left to right|
|Bitwise XOR||^||Left to right|
|Bitwise OR|||||Right to left|
|Logical AND||&&||Left to right|
|Logical OR||||||Left to right|
|Conditional||?:||Right to left|
|Assignment||= += -= *= /= %=>>= <<= &= ^= |=||Right to left|
|Comma||,||Left to right|
String Pattern Programs in C
Java Program to Find pair of Integers in Array whose sum is given Number
Program to Print Diamond Alphabet Patterns in C
Half Diamond Pattern in C using Alphabets
Half Pyramid of Alphabets in C