Upload
others
View
28
Download
0
Embed Size (px)
Citation preview
Operators in C ( use of operators in C )
Operators are the symbol , to perform some
operation ( calculation , manipulation).
&
Set of Operations are used in completion of any
task.
&
C is the Programming language to perform some
task.
2
Operands & Operators
• Any operation is a combination of two things:
1. Operands 2.Operator
• Operators works on operands.
• Operands may be – A Data Value
A Data Variable
A Expression
Examples: 10 + 4 , a - 3 , b + c ,
(a+b)+(b+10)Here, 10,4,3 are Data values and a,b ,c are variables.
3
Types of Operators
On the basis of number of operands, operators are
divided into three types:-
Unary Operators – Acts upon one operand
Binary Operators – Acts upon two operands
Ternary Operators – Acts upon three operands.
4
Types of Operators:-
( On the basis of features of operators)
Increment or Decrement Operator ( Unary)
Arithmetic Operators
Relational Operators
Logical Operators Binary
Assignment Operators
Bitwise Operators
Conditional Operators ( Ternary)
Special Operators
5
Arithmetic Operators
Operator Symbol Action Example
Addition + Adds operands x + y
Subtraction - Subs second from first x - y
Negation - Negates operand -x
Multiplication * Multiplies operandsx * y
Division / Divides first by second x / y
(integer quotient)
Modulus % Remainder of divide op x % y
6
Relational Operators (Relational comparison )
Comparisons can be done with the help of relational operators
These consist of:-
Operator Meaning
< less than
<= less than equal to
> greater than
>= greater than equal to
= = Equal to
!= Not equal to
7
Logical Operators ( Logical comparison )
Expression which combines two or more relational
expression is termed as a logical or compound relational
expression
The result of these operators is either TRUE or FALSE.
Logical expressions are:-
&& logical AND
|| Logical OR
! Logical NOT
Ex- if (age>55 && salary <1000)
8
Assignment Operator
Used to assign the result of an expression to a variable.
ex- int i=20;
1. Simple Assignment e.g. =
2. Compound Assignment e.g. +=, -=, *=, /=, &=
3. Expression Assignment e.g. a=5+(b=8 + (c=2)) - 4
9
Operators continued……..
x + = y+1;
is same as x = x + (y+1)
i.e. += means „add y+1 to x‟
Similarly……..
a=a-1 a - = 1;
a=a*(n+1) a *= n+1;
a=a/(n+1) a/= n+1;
Q: int z=10; z += 34; z *= 2 ; z -=5; z += z+1;
10
Increment and Decrement
C has two very useful operators not generally found in other
languages. These are the increment and decrement operators.
i.e ++ and --
++ operator adds 1 to the operand and -- operator subtracts 1 to the
operand . Both these operators are unary operators and take the
following form.
++m (prefix operator) ; or m++ (postfix operator);
--m; or m--;
++m & m++ means m = m+1;
-- m & m-- means m = m-1;
11
Ex…
int m = 5 , y ;
y = ++m ;
printf ( “ %d%d " y,m );
Result???
int m = 5 , y ;
y = m++ ;
printf ( “ %d%d " y,m );
Result?????
12
Conditional Operator..
A ternary operator pair “?:” is available in C to construct
conditional expressions of the form.
exp1 ? exp2 : exp3;
Operator ?: works as follows: exp1 is evaluated first . If it is
nonzero (true) ,then the expression exp2 is evaluated and
becomes the value of the expression otherwise vice versa.
13
Bitwise Operator….
C has distinction of supporting special operators known as
Bitwise Operators for manipulation of data at bit level. These
are used to test bits.
Operators Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
(Ex-OR)
<< shift left
>> shift right
~ One‟s complement
15
Bitwise Operator….
Examples - int a = 4, b = 3
a = 0000 0100
b = 0000 0011
--------------
a & b = 0000 0000
a | b = 0000 0111
a ^ b = 0000 0111
In Ex-OR (if both bits are same : 0) (if both bits are diff : 1)
a = 10
b = ~ a => ~ (1010) => 0101
16
B Bitwise operators
The shift operator:
x << n
Shifts the bits in x n positions to the left, shifting in zeros on the right.
If x = 1111 1111 1111 00002
x << 1 equals 1111 1111 1110 00002
x >> n
Shifts the bits in x n positions right.
shifts in 0 if it is an unsigned integer
x >> 1 is 0111 1111 1111 10002 (unsigned)
17
Operator Precedence( priority)
Parenthesis
Unary operator ( ~, ++ ,--,-)
Arithmetic ( first - /,%,* then +,-)
Shift Left & Right
Comparison ( first- >,>=,<,<= then ==,!=)
Bitwise AND OR NOT
Logical AND OR
Conditional Operator
Assignment 18
DECREASI
NG
ORDER
Operator Precedence
Operator Precedence Associativity
( ) 1 L to R
~, ++, --, unary - 2 R to L
*, /, % 3 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 L to R
? : 13 R to L
=, +=, -=, etc. 14 R to L
19
Special Operators
• comma(,) {Left to Right}
• Sizeof(parameter) { show the size of given parameter)
• Address operator(&)
• Pointer operator(* )
• Member selection operator( . And ->)
Q: int z= ( a=3 , b = 2 , c=4 , a+b+c);
20
Problems
21
Q1. a = 2 * 3 % 4 + 4 / 4 + 8 – (-2) + 5 / 8
Q2. kk = 3 / 2 * 4 % 3 + 3 / 8 + 3
Q3. int i = 4, j = -1, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "\ny = %d z = %d", y, z ) ;
Problems
22
Q5. int z , x = 5, y = - 10 , a = 4, b = 2
z = x ++ - - y * b / a
Q4. int i = 4, j = -1, k = 0, w, x, y, z ;
w = i || j || k ;
x = i && j && k ;
y = i || j && k ;
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ;
Problems
23
Q6. int a = 10, b
b = a++ + ++ a; Print a , b
Q7. int a = 4; printf(―%d%d‖, a++ + ++a, a++);
Q8. int i = 5;
printf(―%d‖, i = ++i = = 6);
Problems
24
Q9. int a = 3, b
b = ++a + ++ a + ++a; Print a , b
Q10. int a = 4, b ;
b = a++ + a++ + a++; Print a , b
Q11. int i = 5, j ;
j = ++i + i++ + ++i; Print i , j
Problems
25
Q12. Point out the errors, if any—
(a) int 314.562 * 150;
(b) name = ’Ajay’ ;
(c) Varchar = ’3’ ;
(d) 3.14 * r * r * h = vol_of_cyl ;
(e) area = 3.14 * r ** 2;
(f) a = b = 3 = 4 ;
Problems
26
Q13. void main()
{ float a = 5, b = 2;
int c;
c = a % b; printf(‖%d‖, c);
Q14. int c=0,d=5,e=10,a;
a=c>1?d>1||e>1?10:20:30;
printf(―a=%d‖,a);
Problems
27
Q15. int x, y, z ;
x=y=z=1;
z= ++x + ++y + ++z;
printf(―%d%d%d‖,x,y,z);
Q16. #define x 5+2
void main( )
{ int a;
a = x * x * x;
printf(―%d‖, a); }