Lecture CSP04
Lecture CSP04
)
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
2
Bitwise Operators
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
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);
return 0;
}
Increment and Decrement Operators
(Increment or decrementing by one)
6
Pre & Post increment / decrement
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)
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
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.
19
Rules of Evaluation of Expression
1. First, parenthesized sub expression from left to right are
evaluated.
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
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 = ??
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
??