An operator is a symbol that denotes specific mathematical or logical operations. Here are the types of Operators in C/C++.
1. Arithmetic operators
Arithmetic operators are used to perform arithmetic/mathematical operations on operands. Its 2 types are unary operators and binary operators. Unary operators have single operand and Binary operators have two operands.
|+||Addition||a + b||Binary|
|–||Subtraction||a – b||Binary|
|*||Multiplication||a * b||Binary|
|/||Division||a / b||Binary|
|%||Remainder||a % b||Binary|
2. Relational operators
Relational operators are used to compare the values of two operands. They are also called comparison operators. All relational operators can be overloaded in C++. They check the expression and return true or not true. Consider a = 5 and b = 10.
|==||Equality / Equal to||(a == b) — not true|
|!=||Not equal to||(a !=b ) — true|
|>||Greater than||(a > b) — not true|
|<||Less than||(a < b) — true|
|>=||Greater than or equal to||(a >= b) — not true|
|<=||Less than or equal to||(a <= b) — true|
3. Logical operators
Logical operators are used to or more conditions to get the value for original condition/expression. They evaluate and return a boolean value either true or false. Consider x = 15;
|&&||Logical AND||(x > 0 && x<10) — false||Returns true if both statements are true|
|||||Logical OR||(x > 0 || x<10) — true||Returns true if one of the statements is true|
|!||Logical NOT||!(x > 0 && x<10) — true||Reverses the result and returns true if false and false if true.|
4. Bitwise operators
Bitwise operators are used for bit-level operations. They perform operation on each bit of 2 numbers. Consider a = 2, b = 3.
|&||Bitwise AND Operator||a & b = 2|
||||Bitwise OR Operator||a | b = 3|
|^||Bitwise XOR Operator||a ^ b = 1|
|~||Bitwise Not / Complement Operator||~a = 253|
|<<=||Bitwise left shift assignment||4 << 2 = 16|
|>>=||Bitwise right shift assignment||4 >> 1 = 1|
5. Assignment operators
Assignment operators are arithmetic and logical operators which we write in short cut manner. They are also called short hand operators. It combines one of the arithmetic or logical operators with assignment operator. For e.g. a = a + b is written as a+=b.
|=||Direct assignment||x = 5;|
|+=||Addition assignment||x + = 10 ;||x = x + 10 ;|
|-=||Subtraction assignment||x – = 10 ;||x = x – 10 ;|
|*=||Multiplication assignment||x * = 10 ;||x = x * 10 ;|
|/=||Division assignment||x / = 10 ;||x = x / 10 ;|
|%=||Modulus assignment||x % = 10 ;||x = x % 10 ;|
|&=||AND assignment||x &= y;||x = x & y;|
|^=||Exclusive OR assignment operator||x ^= y;||x = x ^ y;|
||=||Inclusive OR assignment operator||x |= y;||x = x | y;|
6. Other operators
|?:||Ternary operator||(condition) ? statement_if_true : statement_if_false||The ternary operator take three arguments. The first is a comparison condition, second is the result for true comparison, third is the result for false comparison.|
|&||Reference||&a||Returns address of variable|
|*||Dereference||*a||Returns value of variable|
|.||Structure reference||a.b||Element selection by reference|
|->||Structure dereference||a->b||Element selection through pointer|
void abc :: xyz()
std :: cout
|Accessing a global variable if there is a local variable with same name, defining a function outside a class, accessing a class’s static variables, referring to a class inside another class, in multiple inheritance and namespace.|
|sizeof()||Size of||sizeof(int) = 4||Return the size of variable or expression between ( )|
|(type)||Type cast||(float) sum||Casts one datatype into another|
|,||Comma||int x=(2,3);||Acts as operator as well as separator. It uses and discards the first value and takes the second value.|
So these were the operators in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.
For daily updates do follow us on Instagram. Visit again!