P. 1
C Programming Ch3

C Programming Ch3

|Views: 11|Likes:
Published by Suraj Mysore

More info:

Published by: Suraj Mysore on Sep 12, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/12/2012

pdf

text

original

Department of MCA

C programming Notes-CH3

KNS Institute of Technology

Chapter-3
Operators and Expressions
C supports a wide variety of operators, such as +, -, *, /, &, <, > etc. In general, An Operator is a symbol that tells the computer to perform certain mathematical of logical manipulations. Operators are used in programs to manipulate data and variables. C operators can be classified into a number of categories. They are: 1. 2. 3. 4. 5. 6. 7. 8. Arithmetic operators. Relational Operators. Logical Operators. Assignment Operators. Increment and Decrement Operators. Conditional Operators. Bitwise Operators. Special Operators.

Arithmetic Operators:
Arithmetic operators include +, -, *, /, %, which performs all mathematical manipulations. These operators can operate on any built-in data type allowed in C. Table shows the function of Arithmetic Operators. Operators + * / % Meaning Addition or unary plus Subtraction of unary minus Multiplication Division Modulo Division

Integer division truncates any fractional part. The modulo division produces the remainder of an integer division. Integer Arithmetic: When both the operands in an expression are integers, then the expression is called an integer expression, and the operation is called integer arithmetic. Eg: Let a=10 and b=6 then a + b = 16 a–b=4
Lecturer: Syed Khutubuddin Ahmed Page 1

33333 Mixed-Mode Arithmetic: This is a combination of integer and float arithmetic. For example. A real operand may assume values either in decimal of exponential notation.0 / 7. price of two items and so on. They are: Operator < > <= >= == != Meaning Is less than Is greater than Is less than or equal to Is greater than or equal to Is equal to Is not equal to Lecturer: Syed Khutubuddin Ahmed Page 2 .Department of MCA C programming Notes-CH3 KNS Institute of Technology a * b = 60 a / b = 1 (decimal part truncated) a % b = 4 (remainder of division) Real Arithmetic: An arithmetic operation involving only real operands is called real arithmetic.9056 Relational Operators: Relational operators are used to compare two operands. b=2. There are 6 types of relational operators. X = 6.0 = -0.0 = 0. then a / b = 13 / 2. Then. y.666667 Z = 1. it can be used to compare the age of two persons.0 = 0. and z are floats.0 / 3. Eg: Let x.857143 Y = -2. where one of the operands in real and the other is integer.65 = 4. and depending on their relation.6. certain decisions are taken.0 / 3. Eg: Let a=13. This type of expression is called a mixed-mode arithmetic expression.

The relational operators are used in decision statements such as. Logical Operators: C supports three Logical Operators. Truth Table for Logical Operators Op-1 Non-Zero Non-Zero 0 0 Op-2 Non-Zero 0 Non-Zero 0 1 0 0 0 Value of the expression Op-1 && Op-2 1 1 1 0 Op-1 || Op-2 Lecturer: Syed Khutubuddin Ahmed Page 3 . Thus. arithmetic operators have a higher priority over relational operators. if.Department of MCA C programming Notes-CH3 KNS Institute of Technology A simple relational expression contains only one relational operator and takes the following form: ae – 1 relational operator ae – 2 The arithmetic expressions on either side of the relational operator are evaluated first and then the results are compared. a > b && x == 10 An expression which combines two or more relational expressions is termed as a logical expression or a compound relational expression. Such as. if else. and while to decide the course of action of a running program. They are: Operator && || ! Meaning Logical AND Logical OR Logical NOT The logical operators && and || are used when we want to test more than one condition and make decisions.

What appears on the left-hand side need not be repeated and therefore it becomes easier to write. is equivalent to : v = v op (exp). Usually. ‘=’ operator is used. There is an additional ‘shorthand’ assignment operators of the form V op = exp. 3. 2. Shorthand Assignment Operators Statement with simple assignment operator a=a+1 a=a–1 a = a * (n+1) a = a / (n+1) a=a%b The advantages of using shorthand assignment operators are: 1. The assignment statement v op = exp. Lecturer: Syed Khutubuddin Ahmed Page 4 Statement with shorthand operator a += 1 a -= 1 a *= n + 1 a /= n + 1 a %= b . If(age > 55 && salary < 1000) 2. If(number < 0 || number > 100) Assignment Operators: Assignment operators are used to assign the result of an expression to a variable. V= variable exp = expression and op = a binary arithmetic operator.Department of MCA C programming Notes-CH3 KNS Institute of Technology Examples: 1. The Operator op= is known as the shorthand assignment operator. The statement is more concise and easier to read. The statement is more efficient.

The operator ++ adds 1 to the operand. we can re-write this as: value(5*j-2) += delta. They are: ++ and -. with the help of += operator. or m++. } } Output : 2 4 16 Increment and Decrement Operators: C supports two unique operators that are not found in other languages. Lecturer: Syed Khutubuddin Ahmed Page 5 .(increment and decrement operators respectively). while(a < N) { printf(“%d\n”. Both are unary operators and take the following form: ++m. a). or m--. /* example program to show the use of shorthand operators */ #define N 100 #define A 2 main() { int a. ++m is equivalent to m = m + 1. We use the increment and decrement operators in for and while loops extensively. --m. a *= a. a = A.Department of MCA Example: C programming Notes-CH3 KNS Institute of Technology value(5*j-2) = value(5*j-2) + delta. --m is equivalent to m = m – 1. while – subtracts 1.

Conditional Operator: A ternary operator pair “? :” is available in C to construct conditional expressions of the form: Exp1? Exp2: Exp3 Exp1. B= 15. If it is nonzero (true). Operator & | ^ Lecturer: Syed Khutubuddin Ahmed Meaning Bitwise AND Bitwise OR Bitwise exclusive OR Page 6 . Bitwise operators may not be applied to float or double. Here only one of the expressions is evaluated. Bitwise Operators: Bitwise operators are special operators that are used for manipulation of data at the bit level. Example: m = n++ .Department of MCA C programming Notes-CH3 KNS Institute of Technology The increment and decrement operators can be used in complex statements. If exp1 is false. The operator ?: works as follows: Exp1 is evaluated first. then exp2 expression is evaluated and becomes the value of the expression. These operators are used for testing the bits. This can be achieved using the if. or shifting them to right or left. ‘n’ is incremented after the evaluation. Some compilers require a space on either side of n++ or ++n.else statement as follows: if (a > b) x = a. Exp2 and Exp3 are expressions. else x = b.j + 10. X will be assigned the value of B. X= (A > B)? A: B. Here. Example: A= 10.. exp3 is evaluated and its value becomes the value of the expression.

m++) In While Loops: while (c = getchar(). The Sizeof Operator Sizeof is a compile time operator and when used with an operand. Applications of Comma operators are: In for loops: for(n = 1. sizeof operator. a constant or a data type qualifier. Note: The comma operator has the lowest precedence of all operators. pointer operators(& and *) and member selection operators (. Example : value = (x = 10. then y is assigned a value 5 and finally (x + y) is calculated and assigns the result to value. m = 10. Lecturer: Syed Khutubuddin Ahmed Page 7 .Department of MCA << >> ~ C programming Notes-CH3 KNS Institute of Technology Shift Left Shift Right One’s Complement Special Operators: C supports some special operators such as comma operator. y = t. The operand may be a variable. x + y). The expressions that used the comma operator are evaluated from left to right and the value of right-most expression is the value of the combined expression. n++. x = y. x is assigned a value 10. it returns the number of bytes the operand occupies.n <= m. c != ‘10’) In exchanging values t = x. In this expression. so the parenthesis is necessary. y = 5. The Comma Operator This operator is used to link the related expressions together. And ).

printf(“a = %d b = %d d = %d\n”. } Output a = 16 b =10 c = 6 a = 16 b = 11 d = 26 a/b = 1 a%b=5 a *= b = 176 0 1 Arithmetic expressions: Expression is a combination of operators. g = sizeof(235L). c = sizeof(long int). The “sizeof” operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. b.(c<d) ? 1 : 0). a. printf(“a *= b = %d\n”. d. printf(“%d\n”. printf(“%d\n”. printf(“a = %d b = %d c = %d\n”.Department of MCA Examples: C programming Notes-CH3 KNS Institute of Technology a = sizeof(sum). constants and variables. Lecturer: Syed Khutubuddin Ahmed Page 8 . c. c = ++a – b. b = 10. b.a%b).(c>d)? 1 : 0). printf(“a / b = %d\n”. a. a = 15. arranged as per the syntax of the language. /* main() { ILLUSTRATION OF OPERATORS */ int a. d). b. printf(“a%%b = %d\n”. An Arithmetic Expression is a combination of constants and arithmetic operators. d = b++ + a.a*=b).a/b). c).

The precedence rule is applied in determining the order of application o Example: Program that illustrates the use of variables in expressions and their evaluation. Lecturer: Syed Khutubuddin Ahmed Page 9 . b. 4. All the variables on the right hand side must be assigned values before evaluation. main() { float a.Department of MCA C programming Notes-CH3 KNS Institute of Technology Example: a * b – c (Algebraic equation) a * b – c (C expression) (ab/c) => a * b / c Evaluation of expressions: Expressions are evaluated using an assignment statement of the form: Variable = expression. y. a = 9. variable  any valid C variable name. x. Rules for evaluation of expression: 1. b=12.  1st expression is evaluated and the result then replaces the previous value of the variable on the left-hand side. Example: y = a * b – c. the given algebraic expression must be converted to a C expression. y = a – b / (3 + c) * (2 – 1). 6.  All variables used in the expression must be assigned values. 1st parenthesized sub expression from left to right is evaluated. where. 2. To evaluate an expression. z. Then it must be written in the form of assignment statement. c=3. If parentheses are nested the evaluation begins with the innermost sub-expression. 3. c. 5. before evaluation is attempted. x = a – b / 3 + c * 2 – 1.

a.e..000000 Z = 4.000000 Precedence of Arithmetic Operators  Any arithmetic expression is evaluated according to the rule of precedence of operators. II. Operators () Unary *.000000 Y = 7. In the case of nested parenthesis (sub-expression). Lecturer: Syed Khutubuddin Ahmed Page 10 .Department of MCA C programming Notes-CH3 KNS Institute of Technology z = a – (b ( 3 + c) * 2) – 1.  An arithmetic expression without parenthesis will be evaluated from Left to Right. 2 is multiplied by 6 and the result 12 is divided by 2. the expression enclosed in parenthesis is evaluated according to the precedence.y). printf(“y = %f\n”. the evaluation is done from Left to Right. } Output X = 10.z). % +Associativity L to R L to R L to R L to R In an arithmetic expression. High Priority */% 2. Evaluation takes place from Left to Right i. 1. the inner most parenthesis is evaluated first and the evaluation continues outwards.. /. There are two distinct priority levels of Arithmetic Operators in C. This is known as Associativity. printf(“x = %f\n”.x). printf(“z = %f\n”. Low Priority +- Hierarchy of Arithmetic Operators: Precedence level 1 2 3 4 I. III. Eg: 2*6/2 i. When the two operators with the same precedence are encountered.

Department of MCA C programming Notes-CH3 =2*6/2 i. Example: 1 X = ((-b+ b2-4ac)/2a) Where. Therefore. b = 4. a = 1. answer IV. c = 4. = 12 / 2 ii. Parenthesis can be used to change the order of evaluation. The equivalent C expression is :X = (-b + sqrt(b*b-4*a*c))/(2 * a) X = ((-4) + sqrt(4 * 4 – 4 * 1 * 4)) / (2 * 1) X = (-4+sqrt(4 * 4 – 4 * 1 * 4)) / (2 * 1) X = (-4+sqrt(16 – 4 * 4)) / (2 * 1) X = (-4 + sqrt (16 – 16)) / (2 * 1) X = (-4 + sqrt(0)) / (2 * 1) X = (-4 + 0) / (2 * 1) X = -4 / (2 * 1) X = -4 / 2 X = -2 (evaluates function) (Outer Parenthesis) Lecturer: Syed Khutubuddin Ahmed Page 11 . = 6 KNS Institute of Technology ii.

then the order of evaluation is from Left to Right.result). (not nested). the left most set is evaluated first and the right most set of parenthesis is evaluated last. printf(“ The given Expression is : (-5*2/(6+4-2) + (2/3 + 5))”). each containing one or more operator The operator with the highest precedence level is evaluated first. Operators in the same level are evaluated wither from left to right or right to left.. clrscr().e. i..h> void main() { int result. Operator Precedence and Associativity The precedence of operators comes into effect whenever there are more than one operators involved in it.h> #include<conio. Lecturer: Syed Khutubuddin Ahmed Page 12 . getch().Department of MCA Example 2: C programming Notes-CH3 KNS Institute of Technology Consider the expression: . result = (-5*2/(6+4-2) + (2/3 + 5)). There are many levels of precedence. } Whenever more than one set of parenthesis appear next to each other. printf(“The result of evaluation : %d “. depending on their Associativity.(-5 * 2/ (6 + 4) – 2) + (2 / 3 + 5) = (-5 * 2 / (6 + 4 – 2) + (2 / 3 + 5)) = (-5 * 2 / (10 -2) + (2 / 3 + 5)) = (-5 * 2 / 8 + (2 / 3 + 5)) = (-5 * 2 / 8 + (0 + 5)) = (-5 * 2 / 8 + 5) = (-10 / 8 + 5) = (-1 + 5) =4 Program : #include<stdio.

when several operators of the same priority level exist in the same expression. Eg: Consider the statement: . Here the Associativity of relational operators has to be considered which is left to right. + operators has the highest priority. b = 1 The order of evaluation will be as follows: Since.Department of MCA C programming Notes-CH3 KNS Institute of Technology Associativity determines the operations to be performed from right and from left. addition is computed first. (a< 10 && 21 > 15)) Both the relational operators have higher priority compared to the logical operator &&. Operator Precedence Table Operator () [] + ++ -! ~ * & Sizeof (type) Description Function call Array element reference Unary plus Unary minus Increment Decrement Logical negation Ones complement Pointer reference (indirection) Address Size of an object Type cast (conversion) Associativity Left to Right Right to Left Rank 1 2 Lecturer: Syed Khutubuddin Ahmed Page 13 . The order of precedence and Associativity of operators are two very important aspects of evaluation. 20 < 10 && 21 > 5  False and True The result of logical && is false or 0.(a < 10 && (a+b) > 15) Where a = 20. in the expression. Thus.

C programming Notes-CH3 Multiplication Division Modulus Addition Subtraction Left shift Right shift Less than Less than equal to Greater than Greater than equal to Equality Inequality Bitwise AND Bitwise XOR Bitwise OR Logical AND Logical OR Conditional expression Assignment Operators KNS Institute of Technology Left to Right 3 Left to Right Left to Right Left to Right 4 5 6 Left to Right Left to Right Left to Right Left to Right Left to Right Left to Right Right to Left Right to Left 7 8 9 10 11 12 13 14 Comma Operator Left to Right 15 Lecturer: Syed Khutubuddin Ahmed Page 14 .Department of MCA * / % + << >> < <= > >= == != & ^ | && || ? = *= /= %= += -= &= ^= |= <<= >>= .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->