Вы находитесь на странице: 1из 41

Operators in C Language

Operator
• Operator is a symbol that operates on a
certain data type.
• The data items that operators act upon are
called operands.
• An expression is a combination of variables,
constants and operators written according to
the syntax of the language.
Types of operators
• Arithmetic
• Relational
• Logical
• Assignment
• Increment/Decrement
• Conditional
• Bitwise
• Special operators
Arithmetic Operators
• Perform arithmetic operations.
• Let A=10, B=20
Operator Meaning Example

+ Addition or unary plus A+B=30

- Subtraction or unary A-B=-10


minus
* Multiplication A*B=200
/ Division A/B=0, B/A=2
% Modulo Division A%B=10, B%A=0
Relational Operators
• Performs comparison between two operands.
• An expression containing a relational operator
is termed as a relational expression.
• Let A=10, B=20
Operator Meaning Example

< is less than


(A < B) is true
<= Less than or equal to
(A <= B) is true
> is greater than
(A > B) is not true
>= greater than or equal to
(A >= B) is not true
== is equal to (A == B) is not true

!= is not equal to
(A != B) is true
Logical Operators
• Combine the results of two relational
expressions.
Operator Meaning Description

&& Logical AND Result is true only


when both
operands are true
|| Logical OR Result is true when
either operand is
true
! Logical NOT Reverses the value
of the operand
Logical AND, OR, NOT
p q p&&q p||q !p !q
false (0) false (0) false (0) false (0) true (1) true (1)
false (0) true (1) false (0) true (1) true (1) false (0)
true (1) false (0) false (0) true (1) false (0) true (1)
true (1) true (1) true (1) true (1) false (0) false (0)

Let i=7,f=5.5
Expression Interpretation Value
(i>=6) && (f<10.5) true 1
f>5 true 1
!(f>5) false 0
(f<11) ||(i<7) false 0

Expression Meaning Value


!15 Is 15 not nonzero i.e. is 15 is equal to 0 0 (false)
15 Is 15 nonzero i.e. is 15 is not equal to 0 1 (true)
Assignment Operators
• Used to assign the result of an expression to a
variable.
– variable=expression
• Multiple assignments
– variable1=variable2=…=expression
• Example:
– a=3
– x=y=z=2 means x, y, z will take the value 2.
Shorthand assignment operators
• +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=
Operator Notation Meaning
+= A+=B A=A+B
-= A-=B A=A-B
*= A*=B A=A*B
/= A/=B A=A/B
%= A%=B A=A%B
&= A&=B A=A&B
^= A^=B A=A^B
|= A|=B A=A|B
<<= A<<=B A=A<<B
>>= A>>=B A=A>>B
Increment/Decrement Operators
• These are unary operators.
• The increment operator (++) causes its
operand to be increased by 1.
• The decrement operator (--) causes its
operand to be decreased by 1.
• ++m (prefix notation)
• m++ (postfix notation)
• --m (prefix notation)
• m-- (postfix notation)
Postfix ++ (or --)
• First the value of variable is used in the
operation and then increment/decrement is
performed
Prefix ++ (or --)
• Prefix ++ (or --): First the value of variable is
incremented/decremented then new value is
used
Increment/Decrement Operators
• Both prefix and postfix notations mean the
same thing when they form statements
independently
• They behave differently when they are used in
expression on the right hand side of an
assignment statement.
Increment/Decrement Operators
• Let us Consider the following:
• m = 5;
• y = ++m; (prefix or pre-increment)
• In this case the value of y and m would be 6
• Suppose if we rewrite the above statement as
• m = 5;
• y = m++; (postfix or post-increment)
• Then the value of y will be 5 and that of m will be 6.
Increment/Decrement Operators
• x=3
• y=x++ + ++x y=8,x=5
• y=x++ + --x y=4,x=3
• y=++x + --x y=6,x=3
• y=x++ + x-- y=6,x=3
• y=++x + x-- y=8,x=3
Conditional Operators
• Also known as ternary true
exp1 exp2
operator.
• exp1 ? exp2 : exp3
• exp1 is evaluated first, if it false
is nonzero(1/true) then the
exp2 is evaluated and this exp2

becomes the value of the


expression,
• If exp1 is false(0/zero)
exp3 is evaluated and its
value becomes the value
of the expression
Conditional Operators
• Example:
• int m=2; int n=3;
• r=(m>n) ? m : n;
• r=3
• a=(b>c?(c>d?12:(d>e?13:14)):15)
• b=5,c=15,d=e=8 a=15
• b=15,c=10,d=e=8 a=12
Bitwise Operators
• Used for manipulation of data at bit level.
• 3 types
– One’s complement operator
– Logical bitwise operators
– Shift operators
One’s complement operator (~)
• Causes the bits of its operand to be converted
so that
– 0s become 1s and 1s become 0s.
• The operand must be an integer type quantity.
• Example
– 3=0000000000000011
– k=~3=1111111111111100
Logical bitwise operators
• 3 types
– Bitwise AND (&)
• Returns 1 if both bits have a value of 1.
– Bitwise OR (|)
• Returns 1 if one or more bits have a value of 1.
• Otherwise, returns 0.
– Bitwise XOR (^)
• Returns 1 if one of bits has a value of 1 and other has a
value 0.
• Otherwise, returns 0.
Logical bitwise operators
b1 b2 b1&b2 b1|b2 b1^b2
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Logical bitwise operators
• Examples
a=4; (0000000000000100)
b=5; (0000000000000101)

c=a&b; (0000000000000100)
What is the value of c? Ans: 4

c=a|b; (0000000000000101)
What is the value of c? Ans: 5

c=a^b; (0000000000000001)
What is the value of c? Ans: 1
Shift operators
• Shift the bits towards left or right.
• Require two operands.
• First operand is the bit pattern to be shifted.
• Second operand is an unsigned integer indicating the
number of displacements.
• Shifting right by n bit positions yields the value by dividing
the number by 2n and truncating the result.
• Shifting left by n bit positions yields the value by
multiplying the number by 2n.
• 2 types
– Left Shift (<<)
– Right Shift (>>)
Left Shift operator (<<)
• Causes all the bits in the first operand to be
shifted towards left by the number of positions
indicated by the second operand.
• The leftmost bits will be lost.
• The rightmost vacant positions will be filled
with 0s for both positive and negative numbers.
• Example: a=13 (0000000000001101)
– c=a<<3=0000000001101000=104 (13*23)
Right Shift operator (>>)
• Causes all the bits in the first operand to be shifted towards
right by the number of positions indicated by the second
operand.
• The rightmost bits will be lost.
• If the number is positive, then leftmost vacant positions
will be filled with 0s.
• Example: a=13 (0000000000001101)
– c=a>>2=0000000000000011=3 (13/22)
• If the number is negative, then leftmost vacant positions
will be filled with 1s.
• Example: a=-3 (1111111111111101)
– c=a>>2=2’s complement of 1111111111111111=
000000000000000001=-1
Special Operators
• Comma operator (,)
• sizeof operator – sizeof()
• Pointer operators – (& and *)
• Member selection operators – (. and ->)
Comma Operator
• Link the related expressions together.
• Evaluated from Left to Right.
• Ex:
– value=(x=10,y=5,x+y);
• First x is assigned the value 10,
• Then, y is assigned the value 5,
• Finally, x+y is evaluated to 15 and is assigned to
value.
sizeof Operator
• Gives the size of an operand in terms of bytes
occupied in the memory.
• The operand can be variable, constant or data
type.
• Consider the declaration
– int i; float x; char c;
– sizeof(i)=2, sizeof(x)=4, sizeof(c)=1
• Similarly, sizeof(7)=2, sizeof(double)=8
Operator precedence and associativity table
Rules for evaluation of expression
• First parenthesized sub expression from left to right are
evaluated.
• If parentheses are nested, the evaluation begins with the
innermost sub expression
• The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
• The associatively rule is applied when 2 or more operators of the
same precedence level appear in a sub expression.
• Arithmetic expressions are evaluated from left to right using the
rules of precedence
• When parentheses are used, the expressions within parentheses
assume highest priority
Precedence of Operators
• There are 2 different priorities of arithmetic
expression
– High Priority: * / %
– Low Priority: + -

• The equation is evaluated in two passes


– First pass: High priority operators
– Second pass: Low priority operators
Expression: x=9-12/3+3*2-1
• 1st Pass
x=9-4+3*2-1
x=9-4+6-1

• 2nd Pass
x=5+6-1
x=11-1
x=10
Example 1

Evaluate x1=(-b+ sqrt (b*b-4*a*c))/(2*a) @ a=1, b=-5, c=6


=(-(-5)+sqrt((-5)(-5)-4*1*6))/(2*1)
=(5 + sqrt((-5)(-5)-4*1*6))/(2*1)
=(5 + sqrt(25 -4*1*6))/(2*1)
=(5 + sqrt(25 -4*6))/(2*1)
=(5 + sqrt(25 -24))/(2*1)
=(5 + sqrt(1))/(2*1)
=(5 + 1.0)/(2*1)
=(6.0)/(2*1)
=6.0/2 = 3.0
Example 2
Evaluate the expression when a=4
b=a- ++a
=a – 5
=5-5
=0
Type Conversion
• The data type of one operand is converted
into data type of another operand
Type
Conversion

Implicit Explicit

Automatic In Assignments
Implicit Type Conversion
• Implicit type conversion, also known as coercion
• An automatic type conversion by the compiler
• If operands are of different types than lower type is
automatically converted to higher type
• If the types of two operands in an expression are
different, then the types of RHS is converted to the
LHS operand.
• If the RHS is of lower rank then it will be promoted
to the rank of LHS.
• If the RHS is higher dank then it will be demoted to
the LHS.
Automatic

long double

double

float

int

char, short int


In Assignment
• Type of right hand side is converted to type of left hand side

• If right hand operand is lower rank than it will be promoted


– float = int
– int = char

• If right hand operand is higher rank than it will be demoted


– char=int
– int=float
Explicit/Type Casting
• Is done with the help of cast operator

• Cast operator is a unary operator that is used for converting an


expression to a particular data type

• Syntax:
– (datatype) expression

• Ex:
int x,y;
float z;
z=(float)x/y;
Explicit Type Conversion
• There may be a situation where implicit conversion may not solve our purpose.
• Ex:- float z;
int x =20, y=3;
z= x/y;
• The value of z will be 6.0 instead of 6.6
• In these cases we can specify our own conversions known as type casting or
coercion.
• It is a unary operator.
• The syntax is
– (Datatype) expression
• Ex: - z = (float) x / y;
• Now z will have value 6.66
• The cast operator changes the data type of variable x only temporarily for the
evaluation of this expression, everywhere else in the program it will be an int
variable only.
More examples
• (int)20.3
constant 20.3 converted to integer type and fractional part is lost.
• (float) 20/3
20 is converted to float and then divided by 3(result=6.66).
• (float) (20/3)
First 20 is divided by 3 and then result of the whole expression is converted to
float type (6.00)

Вам также может понравиться