Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.

Similar presentations


Presentation on theme: "1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation."— Presentation transcript:

1 1 C Programming

2 2 Operators

3 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.

4 4 Operands The data item that operators act upon are called Operands. a + b Operands Operator

5 5 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.

6 6 Types of Operators:- Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment or Decrement Operator Conditional Operators Bitwise Operators Special Operators

7 7 Arithmetic Operators Operator Symbol Action Example Addition + Adds operandsx + y Subtraction - Subs second from firstx - y Negation - Negates operand-x Multiplication * Multiplies operandsx * y Division / Divides first by secondx / y (integer quotient) Modulus % Remainder of divide opx % y

8 8 Relational Operators Comparisons can be done with the help of relational operators These consist of:- OperatorMeaning <less than <=less than equal to >greater than >=greater than equal to = = Equal to !=Not equal to

9 9 Logical Operators 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)

10 10 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

11 11 Operators continued…….. x + = y+1; is same as x = x + (y+1) i.e. += means add y+1 to x Similarly…….. a=a-1a - = 1; a=a*(n+1)a *= n+1; a=a/(n+1)a/= n+1;

12 12 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+1; --m= m-1;

13 13 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?????

14 14 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.

15 15 Example…. a=10 ; b=15; x=(a>b) ? a : b; It can be written as … if ( a > b ) x =a; else x=b;

16 16 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. OperatorsMeaning &bitwise AND |bitwise OR ^bitwise exclusive OR (Ex-OR) <<shift left >>shift right ~Ones complement

17 17 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

18 18 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 0000 2 x << 1 equals 1111 1111 1110 0000 2 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 1000 2 (unsigned)

19 19 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

20 20 Special Operators…. C supports some special types of operators…. such as comma operators, sizeof operator, pointer operators ( & and * ) and member selection operators (. and -> ). Comma Operator:- The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right- most. Ex:- value = (x=10, y=5, x +y);

21 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 ) ;

22 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 ) ;

23 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);

24 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

25 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 ;

26 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);

27 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); }

28 Problems 28 Q17. void main( ) { int a; a=sizeof( 5.6 ); printf("%d",a); } Q18. int a=0,b=10; if(a=0){ printf("true"); } else{ printf("false"); }


Download ppt "1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation."

Similar presentations


Ads by Google