Operators in C Part 3


Operators in C Part 3 :

Assignment Operators :

Assignment operators are used as part of assignment expressions , which assign the value of an expression to an identifier .
The most commonly used assignment operator is the equal sign(=).
Assignment expressions are written in the form
identifier = expression;
the assignment operator assumes there is a variable name to the left of the expression to its right. The expression may be arithmetic , relational ,logical or another assignment. It takes the value of the expression and store it in the variable .

Examples :
·       A=3
·       X=y
·       Sum=a+b

The assignment operator = and the equality operator == are distinctly different.

An assignment operator is used to assign a value to identifier , whereas the equality operator is used to determine if the two expressions have the same value.

Multiple Assignments:

C allows use of multiple assignment operators .
Multiple assignment operators are evaluated from right to left .

It shall take the form of :
identifier1=identifier2=…….=expression;
This expression is equal to the statement
Identifier1=(identifier2 = expression);

Example program :

Output :

In the second assignment statement , the expression y=6 is evaluated first and then the value of the expression is assigned to the variable z.

Other Assignment operators :

The other assignments are   +=  ,  -=  , *=  ,  /=  , %=  .

The assignment expression   “ expression1+= expression2; ”   is equivalent to the expression:
“ expression1=expression1+expression2; “

for example :  i+=5 which is equivalent to i=i+5  .


Conditional Operator (Or) Ternary operator :

 A conditional operators forms an expression that will be having one of two values , depending on the truth value of another expression .

The expression can be written in the place of an if-else statement . 

A conditional expression is written in the form 
expression1 ? expression2 : expression3 ;
when evaluating a conditional expression  , expression1 is evaluated first . if expression1 is true (i.e, if the value is non zero), then expression2 is evaluated and this becomes the value of the conditional expression.

Example program :

Output :





Bitwise operator :

Bitwise operation refers to testing, setting or shifting the actual bits in a byte or a word , which corresponds to the char and int data types and variants.

We cannot use bitwise operation on float, double ,long double ,void, bool or other types.

Operator
Action
&
AND
|
OR
^
Exclusive OR(XOR)
~
One’s complement(NOT)
>> 
Shift right
<< 
Shift left

Other Operators :
Comma (,) operator
Used during sequence of operations
Dot(.) and Arrow (->) operators
These operators access individual elements.
Square bracket[] operator
It  performs array indexing



Thank you ….

Previous Post
Next Post
Related Posts

0 comments:

Popular Posts