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 ….

Operators in C Part 2


Operators in C Part 2 :

This post is a continuation of previous post , link to previous post : https://iamafutureprogrammer.blogspot.com/2019/05/operators-in-c-part-1.html

operators in c


Relational and Logical Operators :

Relational Operators are used in control constructs such as if and while .

There are four Relational operators in  C . They are :
Operators
Meaning
< 
Less than
<=
Less than or equal to
> 
Greater than
>=
Greater than or equal to

All the operators falls within the same precedence group , and their associativity is from left to right.

The two equality operators are :
Operators
Meaning
==
Equal to
!=
Not equal to

Equality operators fall in to a separate group and their associativity is from left to right .Relational operators are used to form logical expressions that compare the value of one expression with another. These logical expressions have a value of zero when the comparison is false and a value of one when the comparison is true .
If i , j and k are integer variables having values  1, 2 and 3 respectively.
Then
Expression
Interpretation
value
i < j
True
1
(i+j)>=k
True
1
(j+k)>(i+5)
False
0
k!=3
False
0
j==2
True
1


Logical Operators :

The C language operators allow a programmer to combine simple relational expression to form complex expressions by using logical NOT , AND and  OR .
Operator
Meaning
&&
AND
||
OR
!
NOT

Taking the logical AND , when two expressions are added the resulting expression will be true only if both the sub expressions are true .

E.g, (x>5)&&(y<15) will be only true if x greater than 5 and if y less than 15 .
The second expression (y<5) will not be evaluated if the first expression is false.

A logical expression is evaluated from left to right as far as needed to determine the logical result .

E.g, in the expression (y<15)&&(x++>15) , the value of x is incremented only if the value of y is less than 15.
The logical OR is represented by two vertical bars between two other expressions as (expression1)||(expression2) . the result of the expression is true if either of the two sub expressions is true .

If i is a integer variable having the value 7 , and f is a floating point variable whose value is 5,5 and c is a character variable that represents the character ‘w’ then:
Expression
Interpretation
Value
(i>=6)&&(c==’w’)
true
1
(i>=6)||(c==119)
true
1
(i<11)&&(i>100)
false
0
(c!=’p’)||((i+f)<=10)
true
1

The NOT simply reverses the truth value of the expression that follows it.

Example:
If i is a integer variable with value 7 and  f is a floating point variable whose value is 5.5 then :
Expression
Interpretation
Value
f>5
true
1
!(f>5)
false
0
!(i>(f+1))
false
0


Lets continue the operators in C in our next post ….

Thank you …..

Operators in C Part 1


Operators in C Part 1 :

As discussed in our previous posts ,individual constants , variables can be joined by various operators to form expressions .

C includes a number of operators as follows :
·       Arithmetic operators
·       Unary operators
·       Relational and Logical operators
·       Assignment operators
·       Conditional operators
·       Bitwise operators

The data item that operator act upon are called operands. Some operands requires two operators while others act upon only one operand.

Arithmetic Operators:

Below are the arithmetic operators and their purpose.
OPERATOR
PURPOSE
+
Addition
-
Subtraction
*
Multiplication
/
It takes quotient after division
%
It takes remainder after division

Examples:

If a and b are two integer variables and there values are 10 and 3 ,then
EXPRESSION
VALUE
a + b
13
a – b
7
a * b
30
a / b
3
a % b
1


If c and d are two floating point variables and there values are 12.5 and 2.0
c + d
14.5
c - d
10.5
c * d
25.0
c / d
6.25
c % d
Not possible

Note:  the modulo (%) operator requires both the operands to be integer and the second operand to be non zero.

If e and f are two character variables that represents the values for e and f as  P and T , then
e
80
e + f
164
e + f + 5
169
e + f +’5’
217


Unary operators :

Unary operators are a class of operators that acts upon single operand to produce a new value.

The common unary operators are :
OPERATOR
What is it
-
Unary minus
++
Increment operator
--
Decrement operator
sizeof
Size of the operand

Example of unary minus :
If a=-4 , b=5 and c=-6 and a=-a , b=-b ,c =-c then ,the values of a , b and c would be 4, -5 and  6 respectively.

A ++ operator increments the value of the operand by 1(i.e , adding the value of operand by one).
If the operator precedes the operand (e.g, i++) then ,the operand will change its value before it is utilized for the intended purpose with in the program. The operators are called pre increment operators.
If the operator follows the operand (e.g.i++),then the value of the operand is altered after it is utilized. It is called as post increment operator.
Example program for pre increment operator :
pre increment operator

Output:


Example program for post increment operator:

Output:


Example program for size of operator:

Output:
Note : The size of operator returns the size in bytes taken by each data type to store its value. The size changes depends on the compiler.

The associativity of the unary operator is from right to left.
For example :
If x and y are integer variables whose values are 10 and 20. The value of the expression  –x+y will be -10+20=10.
The unary minus operation is carried out before the addition operation.
Using the same expression if parenthesis are used , so the expression becomes –(10+20)=  -30. The addition now precedes the unary minus operation.


Lets continue operators in C in our next post .

Thank you..

for more posts on c programming : https://iamafutureprogrammer.blogspot.com

Popular Posts