0% found this document useful (0 votes)
18 views

Lecture CSP04

Uploaded by

physics lover
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture CSP04

Uploaded by

physics lover
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Operators and Expressions (contd..

)
Arithmetic Operators
 Relational Operators
Assignment Operators
 Logical Operators
 Bitwise Operators
 Increment and Decrement Operators
 Conditional Operators
 comma operator
 Special Operators
Precedence and Associativity of Operators
Examples

1
Logical Operators

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Used to test more than one condition

Example: a<b && y==5


- Expression is TRUE only if a<b is TRUE
and y==5 is TRUE
- If either or both of them are FALSE
then expression becomes FALSE

2
Bitwise Operators

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right

 Manipulation of data is at bit level


 May not be applied to float values

3
//example to demonstrate shift operator

#include <stdio.h>
int main()
{ int x=4;
printf("\n value of x is : %d", x);
printf(" \nShifting of x by one bit is");
printf("\n left shift by 1 bit : x << 1 = %d", x << 1);
printf("\n right shift by 1 bit : x >> 1 = %d", x >> 1);

// shift a by two bit


printf(" \nShifting of x by two bit is");

printf("\n left shift by 1 bit : x << 2 = %d", x << 2);


printf("\n right shift by 1 bit : x >> 2 = %d", x >> 2);

return 0;
}
Increment and Decrement Operators
(Increment or decrementing by one)

 It is unary operator that increases/decreases value of its


operand by 1.
Symbol-: ++ or --
 Eg. X=X+1 ; or X++ ;
++i is equivalent to: i = i+1
--i is equivalent to: i = i-1
Is ++i and i++ are same??

++X Pre Increment / Prefix


X++ Post Increment /Postfix

 ++ Adds one to the operand


 -- Subtracts one from the operand
 Both are unary operators
5
What is Postfix and Prefix?
In postfix the expression is evaluated first using the
original value of the variable and then the variable is
incremented (or decremented) by one.
Example.
m = n++;

In prefix the variable is incremented (or decremented)


first and then the expression is evaluated using the new
value of the variable.
Example.
m = --n;

6
Pre & Post increment / decrement

Pre increment: ++x


• Here first add value by 1 to x and then gets value of x;
• Value of x returned after it is incremented

Post increment: x++


• Here first get the value of x and then add value by 1 to x;
• Value of x returned before it is incremented

Example
Y= X++;
here Y is assigned the original value of X, not the incremented
value.
Post fix::
X = a++;
How evaluates??
X=a;
First action: Store the value of ‘a’ in memory location for
variable x.
a= a+1;
second action: increment value of ‘a’ by one and put the
result in memory location for variable ‘a’.

Example:
Y= b--;
Evaluates as :
Y=b;
b=b-1;
Prefix::
X=++a;
How evaluates??
a=a+1;
First action: increment value of ‘a’ by 1 and store result in
memory location of variable ‘a’.
X=a;
second action: store the value of ‘a’ in memory location for
variable ‘x’.

Example:
Y= --b;
Evaluates as :
b=b-1;
Y=b;
Example:

main()
{
int w, x, y, z, result;
w=x=y=z=1;
result = ++w; ??
result=x++; ???
result=--y; ??
result= z--; ???
}
Example answer:
main()
{
int w, x, y, z, result;
w=x=y=z=1;
result = ++w; ?? 2
result=x++; ??? 1
result=--y; ?? 0
result= z--; ??? 1
}
Postfix and Prefix Examples

j =10;
i =j++;
What is the values of i and j?

j =10;
i=++j;
What is the values of i and j?
Conditional Operator ( i.e. ? and :)
 Also called Ternary operator
Syntax
expression1 ? expression2 : expression3

Example:
a=5; b=10;
x =(a>b) ? a : b;
What will be the value of x??

a=5,b;
b=((a==5)?(3):(2)); What will be the value of b??

X=5; Y=10;
(X>Y)? ( printf(“X larger”): printf(“Y is larger”) ;
What will be output?? 13
Special Operators
The Size of Operator ( sizeof() )
--Returns the number of bytes the operand occupies

Examples:
p=sizeof(value);

q=sizeof(float);

r=sizeof(long int);

14
The comma operator
• Used to link the related expressions together.
•Evaluation starts from left to right and
• the value of the right most expression is the value of the combined
expression i.e.
• Rightmost expression becomes the result of whole expression
 It also works as a separator

Example 1:
value = (a = 6, b = 7, c = 8, a+b+c);
output: Value contains 21
Example 2:
int P, Q;
P= 5,10,20; (here, = has more priority then , hence P will be 5)

Q=(5,10,20); (here, () has more priority then ,)

15
Example Program
/*Program to evaluate 5x2+4x+6 */

#include<stdio.h>
int main()
{ int x, result; /* variable declaration */
printf(“Enter value of x\n”);
scanf(“%d”,&x);
result = 5*x*x + 4*x + 6;
printf(“\n Result is %d”, result);
return 0;
}

16
Take home Programming Examples

Write a C program to evaluate following arithmetic


expressions.

1. (a+b)(c+d)
2. ax2+bx+c
3. pr2+2prh
4. s = ut + 1/2at2
5. T = (m1m2/m1+m2)

17
Precedence and Associativity of Operators

18
What is Precedence and Associativity?
• Operators have rules that are used to determine how
expressions are evaluated.

• Precedence and associativity deal with the evaluation order


within expressions

• Precedence rules specify the order in which operators of


different precedence level are evaluated

• Associativity rules decides the order in which multiple


occurrences of the same level operator are applied.

19
Rules of Evaluation of Expression
1. First, parenthesized sub expression from left to right are
evaluated.

2. If parentheses are nested, the evaluation begins with the


innermost sub expression.

3. The precedence rule is applied in determining the order of


application of operators in evaluating sub-expressions.

4. The associativity rule is applied when two or more operators


of the same precedence level appear in a sub expression.

20
Precedence of Arithmetic Operators
Two priority levels of operators
For example:
High priority
*, /, %
Low priority
+ -
Consider the expression : 1+2*3
* has higher priority than +
So the value of the expression is 7

21
Precedence of Arithmetic Operators
 Priority can be overruled by parenthesis
(1+2)*3
 Expression inside parenthesis are evaluated first so
(1+2)*3 gives 9

Eample: Now consider the expression 1+2-3+4-5


Note: + and – have the same precedence
 Associativity rule is used to determine how to evaluate the
above expression.
 Associativity for Arithmetic Operators is L to R

Solve: X = 10 - 16/4 + 4 * 3 - 4
22
precedence and associativity of C operators
Operators Associativity Rank
(),[] L to R 1
+, -, ++, --, !, ~, *, &, sizeof, (type) R to L 2
*, /, % L to R 3
Binary plus(+) and Binary minus (-) L to R 4
<<, >> L to R 5
<, <=, >, >= L to R 6
==, != L to R 7
& L to R 8
^ L to R 9
| L to R 10
&& L to R 11
|| L to R 12
?: R to L 13
=, *=, /=, %=, +=, -=, &=, ^= R to L 14
, L to R 15
23
Examples:
Evaluate the following expressions

z = 2*3/4+4/4+8-2+5/7
z = ??
y = 4/5*6+2/7+4
y = ??

Let int a,b;


float c,d;
a=5;b=7;
c=4.0;d=3.0;
Now compute
x = a/b*(c+d)/a-b+c/d

24
/* program for extracting right most digit from an
integer number*/

#include<stdio.h>
int main()
{ int num,d;
printf(“Enter an integer number:”);
scanf(“%d”,&num);
d = num%10;
printf(“\nRight most digit of the entered number is:”,d);
return 0;
}

25
Question
??

You might also like