C Programming : Operators and Expressions introduction, examples and interview questions

OPERATORS
C operators are the symbols which are used to perform the mathematical and logical operations. C has a rich set of built-in operators. These are the following built-in operators in c:
➣Arithmetic Operators
➣Logical Operators
➣Relational Operators
➣Assignment Operators
➣Bitwise Operators
➣Conditional Operators
➣Increment/Decrement Operators
➣Special Operators


Arithmetic Operator :
These operators are used to perform mathematical operations like addition, subtraction, multiplication, division and modulus.

Operator Description Example
+ Two operands are added 2+3=5
- First operand is subtracted by second operand 3-2=1
* Multiplies two operand 3*2=6
/ Divide numerator by denominator 6/2=3
% Gives remainder after the division of integer 5%2=1


Example:


Logical Operator :
These operators are used to perform logical operations on the given expression.


OperatorDescriptionExample
&&Called as Logical AND operator.
Output is 1 only when the inputs on both the sides will be true
0&&1=0
||Called as Logical OR operator.
Output is 0 only when the inputs on both the sides will be zero
1||0=1
!Called as Logical NOT operator.
Gives the inverted output
!1=0



Example: 




Assignment Operator :
These operators are used to assign the values to the variable.


OperatorDescriptionExample
=Assignment Operator.
Assign right hand side value to the left hand side variable.
z=x+y

+=
Add and Assignment Operator.
Add right hand side operand to the left hand side operand and assigns the result to the left hand side operand.
x+=y
gives
x=x+y
-=Subtract and Assignment Operator.
Subtracts right hand side operand from the left hand side operand and assign the result to the left hand side operand.
x-=y
gives
x=x-y
*= Multiply and Assignment Operator.
Multiply right hand side operand to the left hand side operand and assigns the result to the left hand side operand.
x*=y
gives
x=x*y
/=Divide and Assignment Operator.
Divides right hand side operand from the left hand side operand and assign the result to the left hand side operand.
x/=y
gives
x=x/y
%= Modulus and Assignment Operator.
Divide right hand side operand from the left hand side operand and assigns the remainder to the left hand side operand.
x%=y
gives
x=x%y


Example: 



Relational Operator :
These operators are used to compare two variables or values.

OperatorDescription
==Equal to Operator.
Checks equal or not if true then returns true else return false.
!=Not equal to Operator.
Checks equal or not if equal then returns false else return true.
<Less than Operator.
Checks for lower value.
>Greater than Operator.
Checks for greater value.
<=Less than equal to Operator.
Checks for lower value or equal value.
>=Greater than equal to Operator.
Checks for greater value or equal value.

Example: 


Bitwise Operator :
These operators are used to convert values into binary digits.




OperatorDescriptionExample
>>Right Shift
All bits in x shifted to y bits right in x.
x>>y
<<Left Shift
All bits in x shifted to y bits left in x.
x<<y
&Bitwise AND
perform AND operation between each bit of x and each bit of y.
x&y
|Bitwise OR
Perform OR operation between each bit of x and each bit of y.
x|y
^Bitwise XOR
Perform XOR operation between each bit of x and each bit of y.
x^y




Conditional Operator :

This operator is used to give the result based on condition. It is just like "if-else".

Syntax: ?::

Example:   a>b?c::d  
                10>4?5::6

No comments:

Powered by Blogger.