Operators in C/C++

Operators in C/C++

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.

+Additiona + bBinary
Subtractiona – bBinary
*Multiplicationa * bBinary
/Divisiona / bBinary
%Remaindera % bBinary
++Incrementy++ ;Unary
Decrementy– ;Unary

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) — falseReturns true if both statements are true
||Logical OR(x > 0 || x<10) — trueReturns true if one of the statements is true
!Logical NOT!(x > 0 && x<10) — trueReverses 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 Operatora & b = 2
|Bitwise OR Operatora | b = 3
^Bitwise XOR Operatora ^ b = 1
~Bitwise Not / Complement Operator~a = 253
<<=Bitwise left shift assignment4 << 2 = 16
>>=Bitwise right shift assignment4 >> 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 assignmentx = 5;
+=Addition assignmentx + = 10 ;x = x + 10 ;
-=Subtraction assignmentx – = 10 ;x = x – 10 ;
*=Multiplication assignmentx * = 10 ;x = x * 10 ;
/=Division assignmentx / = 10 ;x = x / 10 ;
%=Modulus assignmentx % = 10 ;x = x % 10 ;
&=AND assignmentx &= y;x = x & y;
^=Exclusive OR assignment operatorx ^= y;x = x ^ y;
|=Inclusive OR assignment operatorx |= y;x = x | y;

6. Other operators

?:Ternary operator(condition) ? statement_if_true : statement_if_falseThe 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&aReturns address of variable
*Dereference*aReturns value of variable
.Structure referencea.bElement selection by reference
->Structure dereferencea->bElement selection through pointer
::Scope resolution::a
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 ofsizeof(int) = 4Return the size of variable or expression between ( )
(type)Type cast(float) sumCasts one datatype into another
,Commaint 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!



comments user

It’s not my first time to go to see this web page, i am visiting his web site very often and take
good facts from here.

comments user
Disinfection Services

It’s nott my first time to go to see this wweb page, i am visiting
this web sitye very often and take good facts from here.

comments user

I have read several good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a magnificent informative website.

Leave a Reply

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