Upload
vinay-arora
View
4.558
Download
1
Tags:
Embed Size (px)
Citation preview
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.1
C Programming - Operators and Expressions
2
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.2
Programming in C: A Practical ApproachOperators and Expressions
Expressions
•An expression in C is made up of one or more operands.•The simplest form of an expression consists of a single operand. For example, 3 is an expression that consists of a single operand i.e. 3. Such an expression does not specify any operation to be performed and is not meaningful. •For example, a=2+3 // meaningful expressionInvolves three operands :a, 2 and 3Two operators i.e. = (assignment operator) and + (arithmetic addition operator). Thus, an expression is a sequence of operands and operators that specifies the computation of a value.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.3
Programming in C: A Practical ApproachOperators and Expressions
Operands
•An operand specifies an entity on which an operation is to be performed. •An operand can be a variable name, a constant. •For example, a=b+2 is a valid expression involving three operands namely,
•a variable name i.e. a,
•a variable name i.e. b,
•a constant i.e. 2.
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.4
Operators
•An operator specifies the operation to be applied to its operands. •For example, the expression:a=b+2; involves two operators namely,• arithmetic addition operator i.e. +• assignment operator i.e. =. •Based on the number of operators present in an expression, expressions are classified as simple expressions and compound expressions.
Simple and Compound Expressions
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.5
Simple Expressions1. An expression that has
only one operator is known as simple expression.
2. E.g., a+2 is a simple expression.
3. Evaluation of simple expression is easier as order of determination is trivial in this case as there is only one operator.
Compound Expressions1. An expression that involves
more than one operator is called compound expression.
2. E.g. b=2+3*5 is a compound expression.
3. Whereas the evaluation of a compound expression requires the correct order in which the operators will operate. This depends upon the Precedence and the Associativity of operators.
Precedence of Operators
•Each operator in C has a precedence associated with it.•In a compound expression, if the operators involved are of different precedence, the operator of higher precedence is evaluated first.
•For example, in an expression b=2+3*5,
•The sub-expression 3*5 involving multiplication operator (i.e. *).
•The result of evaluation of an expression is an r-value. The sub-expression 3*5 evaluates to an r-value 15. This r-value will act as second operand for addition operator and the expression becomes b=2+15.
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.6
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.7
• In the resultant expression, the sub-expression 2+15. The expression after the evaluation of the addition operator reduces to b=17. • Now, there is only one operator in the expression. The
assignment operator will operate and the value 17 is assigned to b. • The precedence order is * > + > =.• The knowledge of precedence of operators alone is not
sufficient to evaluate a compound expression in case two or more operators involved are of same precedence. To determine which of these operators will operate first, the Associativity of these operators is to be considered.
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.8
Associativity of Operators
Key Points• An operator can be either left-to-right associative or right-to-left
associative. • The operators with same precedence always have the same
associativity. • If operators are left-to-right associative, they are applied in left-to-
right order i.e. the operator which appears towards left will be evaluated first.
• If they are right-to-left associative, they will be applied in the right-to-left order.
• The multiplication and the division operators are left-to-right associative. Hence, in expression 2*3/5, the multiplication operator is evaluated prior to the division operator as it appears before the division operator in left-to-right order.
Classification of Operators
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.9
Programming in C: A Practical ApproachOperators and Expressions
TERNARY OPERATOR
BINARY OPERATOR
UNARY OPERATORBASED ON NO.
OF OPERATORS
BASED ON ROLE OF
OPERATOR
BITWISE OPERATORS
MISCELLANEOUS OPERATORS
ARITHMETIC OPERATORS
ASSIGNMENT OPERATORS
LOGICAL OPERATORS
RELATIONAL OPERATORS
CLASSIFICATION OF OPERATORS
Based on No. of OperatorsUnary Operators• Unary operator operates on only one operand. • For example, in the expression -3,• - is unary minus operator as it operates on only one operand i.e. 3.
The operand can be present towards the right of unary operator, as in -3 or towards the left of unary operator, as in the expression a++.
• Examples of unary operators are:1. & (address-of operator)2. sizeof operator3. ! (logical negation)4. ~ (bitwise negation)5. ++ (increment operator)6. -- (decrement operator) etc.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.10
Programming in C: A Practical ApproachOperators and Expressions
Binary Operators
• Binary operator operates on two operands. It requires an operand towards its left and right.
• For example, in expression 2-3• - is acting as binary minus operator as it is operating on two
operands i.e. 2 and 3. • Examples of binary operators are:
1. * (multiplication operator),2. / (division operator),3. << (left shift operator),4. == (equality operator),5. && (logical AND),6. & (bitwise AND) etc.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.11
Programming in C: A Practical ApproachOperators and Expressions
Ternary Operators
• Ternary operator operates on three operands. • Conditional operator (i.e. ? :) is the only ternary operator
available in C.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.12
Programming in C: A Practical ApproachOperators and Expressions
Based on Role Of OperatorArithmetic Operators
The arithmetic operations like addition, subtraction, multiplication, division etc. can be performed by using arithmetic operators. Following arithmetic operators are available in C:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.13
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator
Name of Operator
Category -ary of Operators
Precedence amongst
arithmetic class
Associativity
1. +-
++--
Unary plusUnary minusIncrement Decrement
Unary operators
Unary Level-I (Highest)
R→L (Right-to-left)
2. */%
Multiplication
DivisionModulus
Multiplicative operators
Binary Level-II (Intermediate)
L→R(Left-to-right)
3. +-
AdditionSubtraction
Additive operators
Binary Level-III(Lowest)
L→R
The operators within a row have the same precedence and the order in which they are written does not matter.
Key Points: Arithmetic Operators
• The parenthesized sub-expressions are evaluated first.• If the parentheses are nested, the innermost sub-expression is
evaluated first.• The precedence rules are applied to determine the order of
application of operators while evaluating sub-expressions.• The associativity rule is applied when two or more operators of
the same precedence appear in the sub-expression.• If the operands of a binary arithmetic operator are of different
but compatible types, C automatically applies arithmetic type conversion to bring the operands to a common type. This automatic type conversion is known as implicit type conversion. The result
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.14
Programming in C: A Practical ApproachOperators and Expressions
of evaluation of operator will be of common type. The basic principle behind the implicit arithmetic type conversion is that if operands are of different types, the lower type (i.e. smaller in size) should be converted to a higher type (i.e. bigger in size) so that there is no loss in value or precision. Since, a lower type is converted to a higher type, it is said that lower type is promoted to a higher type and the conversion is known as promotion.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.15
Programming in C: A Practical ApproachOperators and Expressions
Arithmetic Type Conversions
• If one operand is long double, the other will be converted to long double and the result will be long double. • If one operand is double, the other will be converted to double
and the result will be double.• If one operand is float, the other will be converted to float and
the result will be float. • If one of the operands is unsigned long int, the other will be
converted to unsigned long int and the result will be unsigned long int.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.16
Programming in C: A Practical ApproachOperators and Expressions
• If one operand is long int and other is unsigned int then • If unsigned int can be converted to long int, then unsigned int
operand will be converted as such and the result will be long int. • Else, both operands will be converted to unsigned long int and
the result will be unsigned long int. • If one of the operands is long int, the other will be converted to
long int and the result will be long int.• If one operand is unsigned int, the other will be converted to
unsigned int and the result will be unsigned int.• If none of the above is carried out, both the operands are
converted to int.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.17
Programming in C: A Practical ApproachOperators and Expressions
Key Points: Arithmetic Operators
1. The unary plus operator can appear only towards the left side of its operand.2. The unary minus operator can appear only towards the left side of its
operand.3. Increment operator
• The increment operator can appear towards the left side or towards the right side of its operand. If it appears towards the left side of its operand (e.g. ++a), it is known as pre-increment operator. If it appears towards the right side of its operand (e.g. a++), it is known as post-increment operator.
• The increment operator can only be applied to an operand that has a modifiable L-value. If it is applied to an operand that does not have a modifiable L-value, there will be “L-value required” error.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.18
Programming in C: A Practical ApproachOperators and Expressions
• ++a or a++ is equivalent to a=a+1.• The difference between pre-increment and post-increment
lies in the point at which the value of their operand is incremented. • In case of pre-increment operator, first the value of its operand is
incremented and then it is used for the evaluation of expression. • In case of post-increment operator, the value of operand is used
first for the evaluation of the expression and after its use, the value of the operand is incremented. • Increment operator is a token i.e. one unit. There should be no
white space character between two ‘+’ symbols. If white space is placed between two ‘+’ symbols, they become two unary plus (+) operators.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.19
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.20
Programming in C: A Practical ApproachOperators and Expressions• Tokens are the basic building blocks of a source code.
Characters are combined into tokens according to the rules of the programming language. There are five classes of tokens: identifiers, reserved words, operators, separators and constants.
4. Decrement operator• The decrement operator can appear towards the left side or towards the
right side of its operand. If it appears towards the left side of its operand (e.g. --a), it is known as pre-decrement operator. If it appears towards the right side of its operand (e.g. a--), it is known as post-decrement operator.
• The decrement operator can only be applied to an operand that has a modifiable L-value. If it is applied on an operand that does not have a modifiable L-value, there will be a compilation error “L-value required”.
• --a or a-- is equivalent to a=a-1.• The difference between pre-decrement and post-decrement
lies in the point at which the value of their operand is decremented. • In case of pre-decrement operator, firstly the value of its operand
is decremented and then used for the evaluation of the expression in which it appears. • In case of post-decrement operator, firstly the value of operand is
used for the evaluation of the expression in which it appears and then, its value is decremented.
• Decrement operator is a token i.e. one unit. There should be no white space character between two ‘-’ symbols. If white space is placed between two ‘-’ symbols, they become two unary minus (-) operators.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.21
Programming in C: A Practical ApproachOperators and Expressions
5. Division operator• The division operator is used to find the quotient.
• The sign of the result of evaluation of division operator depends upon the sign of both numerator as well as denominator. If both are positive, the result will be positive. If both are negative, the result will be positive. If either of the two is negative, the result will be negative. For example: 4/3=1, -4/3=-1, 4/-3=-1 and -4/-3=1.
6. Modulus operator• The modulus operator is used to find the remainder.
• The operands of modulus operator (i.e. %) must be of integer type. Modulus operator cannot have operands of floating point type.
• The sign of the result of evaluation of modulus operator depends only upon the sign of numerator. If sign of numerator is positive, the sign of result will be positive else negative. For example: 4%3=1, -4%3=-1, 4%-3=1 and -4%-3=-1.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.22
Programming in C: A Practical ApproachOperators and Expressions
Relational Operators
Relational operators are used to compare two quantities (i.e. their operands). There are six relational operators in C:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.23
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator Name of Operator
Category -ary of Operators
Precedence amongst relational
class
Associativity
1. <> <=
>=
Less thanGreater thanLess than or
equal toGreater than or
equal to
Relational operators
Binary Level-I L→R
2. ==!=
Equal toNot equal to
Equality operators
Binary Level-II L→R
Key Points: Relational Operators
• There should be no white space character between two symbols of a relational operator.
• The result of evaluation of a relational expression (i.e. involving relational operator) is a boolean constant i.e. 0 or 1.
• Each of the relational operators yields 1 if the specified relation is true and 0 if it is false. The result has type int.
• The expression a<b<c is valid and is not interpreted as in ordinary mathematics. Since, less than operator (i.e. <) is left-to-right associative, the expression is interpreted as (a<b)<c. This means that “if a is less than b, compare 1 with c, otherwise, compare 0 with c”.
• An expression that involves a relational operator forms a condition. For example, a<b is a condition.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.24
Programming in C: A Practical ApproachOperators and Expressions
Logical Operators
Logical operators are used to logically relate the expressions. The C language has the following logical operators:
In C language, there is no operator available for logical eXclusive-OR (XOR) operation.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.25
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator Name of Operator
Category -ary of Operators
Precedence amongst
logical class
Associativity
1. ! Logical NOT Unary Unary Level-I R→L
2. && Logical AND Logical operator
Binary Level-II L→R
3. || Logical OR Logical operator
Binary Level-III L→R
Key Points: Logical Operators
• Logical operators consider operand as an entity, a unit.• Logical operators operate according to the following truth
tables:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.26
Programming in C: A Practical ApproachOperators and Expressions
OPERAND 1 OPERAND 2 AND OPERATION
OR OPERATION
FALSE FALSE FALSE FALSE
FALSE TRUE FALSE TRUE
TRUE FALSE FALSE TRUE
TRUE TRUE TRUE TRUE
OPERAND NOT OPERATION
FALSE TRUE
TRUE FALSE
• If an operand of a logical operator is a non-zero value, the operand is considered as true. If operand is zero, it is considered as false. • Each of the logical operators yields 1 if the specified relation
evaluates to true and 0 if it evaluates to false. The evaluation is done according to the truth tables mentioned in previous slide. The result has type int.• The logical AND (i.e. &&) operator and the logical OR (i.e. ||)
operator guarantee left to right evaluation.• Expressions connected by && or || are evaluated left to right
and the evaluation stops as soon as truthfulness or falsehood of the result is
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.27
Programming in C: A Practical ApproachOperators and Expressions
known. Thus, in an expression:a.E1&&E2, where E1 and E2 are sub-expressions, E1 is
evaluated first. If E1 evaluates to 0 (i.e. false), E2 will not be evaluated and the result of overall expression will be 0 (i.e. false). If E1 evaluates to a non-zero value (i.e. true) then E2 will be evaluated to determine the truth value of overall expression.
b.E1||E2, where E1 and E2 are sub-expressions, E1 is evaluated first. If E1 evaluates to a non-zero value (i.e. true), E2 will not be evaluated and the result of overall expression will be 1 (i.e. true). If E1 evaluates to 0 (i.e. false) then E2 will be evaluated to determine the truth value of overall expression.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.28
Programming in C: A Practical ApproachOperators and Expressions
Bitwise Operators
The C language provides six operators for bit manipulation. These operators do not consider operand as one entity and operate on the individual bits of operands. The following bitwise operators are available in C:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.29
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator Name of Operator
Category -ary of Operators
Precedence amongst
bitwise class
Associativity
1. ~ Bitwise NOT Unary Unary Level-I R→L
2. <<>>
Left ShiftRight Shift
Shift operators Binary Level-II L→R
3. & Bitwise AND Bitwise operator Binary Level-III L→R
4. ^ Bitwise X-OR Bitwise operator Binary Level-IV L→R
5. | Bitwise OR Bitwise operator Binary Level-V L→R
Assignment Operators
A variable can be assigned a value by using assignment operator. The following assignment operators are available in C language:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.30
Programming in C: A Practical ApproachOperators and Expressions
S. No Operator Name of Operator Category -ary of Operators
Precedence Associativity
1. =*=/=%=+=-=&=|=^=
<<=>>=
Simple assignment
Assign productAssign quotientAssign modulus
Assign sumAssign difference
Assign bitwise AND
Assign bitwise ORAssign bitwise
XORAssign left shift
Assign right shift
Assignment
Shorthand assignment operators
Binary Level-I R→L
Key Points: Assignment Operators
• The operand that appears towards the left side of an assignment operator should have a modifiable l-value. If the operand appearing towards the left side of the assignment operator does not have modifiable l-value, there will be a compilation error “L-value required”.
• The shorthand assignment is of the form op1 op=op2, where op1 and op2 are operands and op= is a shorthand assignment operator. It is a shorter way of writing op1 = op1 op op2. For example, a/=2 is equivalent to a=a/2.
• There should be no white space character between two symbols of short-hand assignment operators.
• If two operands of an assignment operator are of different types, the type of operand on the right side of assignment operator is
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.31
Programming in C: A Practical ApproachOperators and Expressions
automatically converted to the type of operand present on its left
side. To carry out this conversion, either promotion or demotion is applied.• The terms assignment and initialization are related but it is
important to note the differences between them:
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.32
Programming in C: A Practical ApproachOperators and Expressions
S.No Initialization Assignment
1. First time assignment at the time of definition is called initialization. For example: int a=10; is initialization of a.
Value of a data object after initialization can be changed by the means of assignment. For example: Consider the following statements int a=10; a=20; The value of a is changed to 20 by the assignment statement.
2. Initialization can be done only once. Assignment can be done any number of times.
3. Qualified constant can be initialized with a value. For example, const int a=10; is valid.
Qualified constant cannot be assigned a value. It is erroneous to write a=10; if a is a qualified constant.
Miscellaneous Operators
Other operators available in C are:• Function call operator (i.e. ())• Array subscript operator (i.e. [])• Member select operator • Direct member access operator (i.e. . (dot operator or period))• Indirect member access operator (i.e. -> (arrow operator))• Indirection operator (i.e. *)• Conditional operator• Comma operator• sizeof operator• Address-of operator (i.e. &)
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.33
Programming in C: A Practical ApproachOperators and Expressions
Sizeof Operator• The sizeof operator is used to determine the size in bytes that a
value or a data object will take in memory.
The following are the important points about sizeof operator:• The general form of sizeof operator is:• sizeof expression or sizeof (expression) • (For example: sizeof 2, sizeof(a), sizeof(2+3))• sizeof (type-name) (For example: sizeof(int), sizeof(int*),
sizeof(char))
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.34
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator Name of Operator
Category ary of Operators
Precedence
Associativity
1. sizeof Size-of operator
Unary Unary Level-I R→L
• Parentheses are must if sizeof operator is applied on a type-name, as indicated in the point 1 b) above.• The type of result of evaluation of sizeof operator is int.• The operand of sizeof operator is not evaluated. • The sizeof operator cannot be applied on operands of
incomplete type or function type.
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.35
Programming in C: A Practical ApproachOperators and Expressions
Combined Precedence of All Operators• Till now we have discussed intra-class precedence, now we will discuss the
inter-class precedence
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.36
S.No Operator Name of Operator Category -ary of Operators
Precedence Associativity
1. ()[]->.
Function callArray subscript
Indirect member accessDirect member access
Level-I(Highest)
2. !~+-
++--&*
sizeof
Logical NOTBitwise NOTUnary plus
Unary minusIncrementDecrementAddress-ofDeference
Sizeof
Unary Unary Level-II R→L
3. */%
MultiplicationDivisionModulus
Multiplicative
operators
Binary Level-III L→R
Programming in C: A Practical ApproachOperators and Expressions
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.37
S.No Operator Name of Operator Category ary of Operators
Precedence Associativity
4. +-
AdditionSubtraction
Additive operators
Binary Level-IV L→R
5. <<>>
Left ShiftRight Shift
Shift operators
Binary Level-V L→R
6. <> <=>=
Less thanGreater than
Less than or equal toGreater than or equal to
Relational operators
Binary Level-VI L→R
7. ==!=
Equal toNot equal to
Equality operators
Binary Level-VII L→R
8. & Bitwise AND Bitwise operator
Binary Level-VIII L→R
9. ^ Bitwise X-OR Bitwise operator
Binary Level-IX L→R
10. | Bitwise OR Bitwise operator
Binary Level-X L→R
11. && Logical AND Logical operator
Binary Level-XI L→R
12. || Logical OR Logical operator
Binary Level-XII L→R
13. ?: Conditional operator Conditional Ternary Level-XIII R→L
Programming in C: A Practical ApproachOperators and Expressions
S.No Operator Name of Operator Category ary of Operators
Precedence Associativity
14. =*=/=%=+=-=&=|=^=
<<=>>=
Simple assignmentAssign productAssign quotientAssign modulus
Assign sumAssign difference
Assign bitwise AND
Assign bitwise ORAssign bitwise
XORAssign left shift
Assign right shift
Assignment &
Shorthand assignment operators
Binary Level-XIV R→L
15. , Comma operator Comma Binary Level-XV(Least)
L→R
Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.38
Programming in C: A Practical ApproachOperators and Expressions