Prepared by: Shraddha Modi
Introduction Operator: An operator is a symbol that tells the Computer to perform certain mathematical or logical manipulations. Expression: An expression is a sequence of operands and operators that reduces to single value. An expression is a formula consisting of 1 or more operators. The operation are represented by operators. Eg: 10+25 is an expression whose value is 35. 1. Simple Expressions 5+6; or a + b; 2. Complex Expressions a * b/c + d; C operators can be classified into a no. of categories.: 1. Arithmetic 2. Relational 3. Logical 4. Assignment 5. Increment and Decrement 6. Conditional 7. Bitwise 8. Special
Arithmetic Operators These are also called binary operators. These operate on two or more operands. C provides 5 basic binary arithmetic operators as it is requires two values to calculate the final answer. Arithmetic operators are also called as Mathematical Operators. Operator Symbol Addition + Subtraction - Multiplication * Division / Modulo division % % cannot be used for floating point data. Note : During modulo division, sign of the result is always the sign of the first operand (the dividend ).
Integer Arithmetic: When the operands in an expression are integers then the expression is an integer expression and the operation is called integer arithmetic. This always yields an integer value. Real Arithmetic / Floating Pont Arithmetic: Floating Point Arithmetic involves only real operands of decimal or exponential notation. Mixed mode Arithmetic: When one of the operands is real and the other is integer the expression is a mixed mode arithmetic expression.
Relational Operator These are used to compare or determine relation among two operands. Operands can be either constant or variable. These are the operators used to Compare arithmetic, logical and character expressions. The value of a relational express is either one or zero. It is 1(one) if specified relation is true and 0(zero) if it is false. Operator Meaning < is less than < = is less than or equal to > is greater than or equal to > = is greater than or equal to = = is equal to! = is not equal to
Logical Operator It is used to combine two or more relational expressions. Logical Operators are used when we want to test more than one condition and make decisions. Operator Meaning && AND a=5, b=7, c= 9 (a<b) && (a<c) (a>b) && (a<c) True(1) False(0) OR (b<a) (a<c) True(1) (b<a) (c<b) False(0)! NOT!(a<b) False(0)!(a>b) True(1)
A B A&&B A B!A 0 0 0 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0 void main ( ) { clrscr ( ); printf( In 5>3 && 5<10 : %d, 5>3&&5<10); printf( In 8<5 5= =5 : %d, 8<5 5= =5); printf( In!(8 = =8) : %d,!(8= =8)) ; } Output: In 5>3 && 5<10 : 1 In 8<5 5= =5 : 1 In!(8 = =8) : 0
Assignment Operator Used to assign the result of an expression to a variable. = is the assignment operator. In addition C has a set of short hand assignment operators of the form Var Op = Exp ; Operator Symbol Example Result Assignment Operator = a = 10; Addition Assignment Operator += a+=15; a=a+15 Subtraction Assignment Operator -= a-=10; a=a-10 Multiplication Assignment Operator *= a*=n+1 a=a*(n+1) Division Assignment Operator /= a/=n+1 a=a/(n+1) Modulus Assignment Operator %= a%=2; a=a%2
The use of shorthand assignment operators has advantage: 1) what appears on the left-hand side need not be repeated and therefore it becomes easier to write. For example: value(5*j-2) = value(5*j-2) + delta using shorthand assignment operator value(5*j-2) += delta
Increment and Decrement Operators(unary operators ) The increment and decrement operators can be used only with variables, not with constants. The operation performed is to add one to or subtract one from the operand. Operator Increment Operator ++ Decrement Operator -- Meaning ++x or x ++ == > x+=1 == > x=x+1 -- x or x- - == > x-=1 == > x=x-1 A Prefix/pre-increment operator first increment the operand and then the result is assigned to the variable on left. A postfix operator first assigns the value to the variable on the left and the increments the operand. m = 5; m = 5; y = ++m; y = m++ O/P m =6, y=6 O/P m=6, y=5
WAP to show the effect of increment and decrement operators main ( ) { int x = 10, y = 20, z, a ; z= x * y ++; a = x * y ; printf( %d % d\n, z,a); z = x * ++y; a = x * y; printf( %d %d\n, z, a); } OUTPUT: 200 210 220 220
main() { int count = 0, loop; loop = ++count; printf("loop = %d, count = %d\n", loop, count); loop = count++; printf("loop = %d, count = %d\n", loop, count); } Output: loop = 1, count = 1 loop = 1; count = 2
#include<stdio.h> void main() { } int x=1,y=2,z; z=x++ + ++y + y++; printf( z=%d",z); printf( x=%d",x); printf( y=%d",y); Output: z=7 x = 2 y= 4
Conditional Operator The Conditional operator is used to check condition whether true or false. The result will be as per calculated by the statement. Syntax for conditional operators is Expression1? Expression 2 : Expression 3; True Expression 1? Expression 2: Expression 3; False
void main() { int a,b; printf("\n\nenter First No. = "); scanf("%d",&a); printf("\nenter Second No. = "); scanf("%d",&b); (a<b)?printf("\n\n\a\tfirst No. i.e. %d is smaller",a):printf("\n\n\a\tsecond No. i.e. %d is smaller",b); getch(); }
Bitwise operator C Supports a Special operator known as Bitwise operator which provides bit level manipulation. It is used to test the bits or may be shifted right or left. Operator & ^ ~ >> << Description Example: c=a&b, Answer=0 a=1,b=2 so And Operator Or Operator a=0001,b=0010, now 0001 & 0010 = 0000 Excusive OR Operator Compliment Operator Right Shift Operator Left Shift Operator
Bit1 Bit2 Bit1&Bit2 Bit1 Bit2 Bit1 ^ Bit2 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 void main () { int x=15,y=8; printf( Bitwise AND = %d", x&y); printf( Bitwise OR = %d", x y); printf( Bitwise Exclusive OR = %d", x ^ y); printf( Bitwise complement = %d", ~ y); printf( Bitwise left shift = %d", y << 1); printf( Bitwise right shift = %d", y >> 2 ); getch(); } Output: Bitwise AND = 8 Bitwise OR = 15 Bitwise Exclusive OR =7 Bitwise complement = 245 Bitwise left shift = 16 Bitwise right shift = 2
Special operator Comma operator Sizeof operator
Type conversion and Type casting Type conversion : Automatic (lower type to higher type) Rules: 1) If float is assigned to integer, fractional part is truncated. 2) If double is assigned to float, then rounding take place. 3) If long int is assigned to integer, then additional bits are dropped. Type casting : By user Synatx: (typename) expression
Example: void main () { int a = 5,b; float c = 5.5; double d = 4.0; b = a * c + d/10; printf( Ans is = %d", b ); getch(); } Output: Ans is = 27 void main () { int sum = 47,n= 10; float avg; avg = sum / n; printf( Ans is = %f", avg ); avg = (float) sum / n; printf( Ans is = %f", avg ); avg = (float) (sum / n); printf( Ans is = %f", avg ); getch(); } Output: Ans is = 4.0 Ans is = 4.7 Ans is = 4.0