0% found this document useful (0 votes)
25 views

Chapter 2

The document discusses operators and expressions in C. It defines expressions as consisting of operands and operators, with operands specifying values or entities on which operations are performed. Operators specify the operations to be applied to operands. Expressions can be simple, involving a single operator, or compound, involving multiple operators. The order of evaluation depends on operator precedence and associativity. Arithmetic, relational, logical, and bitwise operators are described along with their precedence levels and associativity rules to determine evaluation order in compound expressions. Implicit type conversions may occur when operands in arithmetic expressions have incompatible but convertible types.

Uploaded by

yashoraj2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter 2

The document discusses operators and expressions in C. It defines expressions as consisting of operands and operators, with operands specifying values or entities on which operations are performed. Operators specify the operations to be applied to operands. Expressions can be simple, involving a single operator, or compound, involving multiple operators. The order of evaluation depends on operator precedence and associativity. Arithmetic, relational, logical, and bitwise operators are described along with their precedence levels and associativity rules to determine evaluation order in compound expressions. Implicit type conversions may occur when operands in arithmetic expressions have incompatible but convertible types.

Uploaded by

yashoraj2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

CHAPTER-2

OPERATORS 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 expression
involves three operands :a, 2 and 3
two 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.
OPERANDS
An operand specifies an entity on which an
operation is to be performed.
An operand can be a variable name, a constant, a
function call or a macro name.
For example, a=printf(“Hello”)+2 is a valid
expression involving three operands namely,
• a variable name i.e. a,
• a function call i.e. printf(“Hello”) and
• a constant i.e. 2.
OPERATORS
An operator specifies the operation to be applied to its
operands.
For example, the expression:
a=printf(“Hello”)+2;
involves three operators namely,
 function call operator i.e. ()
 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
SIMPLE EXPRESSIONS COMPOUND EXPRESSIONS
1. An expression that has only 1. An expression that involves
one operator is known as more than one operator is
simple expression . called compound expression.
2. E.g. b=2+3*5 is a compound
2. E.g., a+2 is a simple expression
expression. 3. Whereas the evaluation of a
3. evaluation of simple compound expression requires
expression is easier as order of the correct order in which the
determination is trivial in this operators will operate. This
case as there is only one depends upon the precedence
operator and it has to operate and the associativity of
in any case. 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.
 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.
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.
UNARY
OPERATOR
BINARY
BASED ON OPERATOR
NO. OF ARITHMETIC
TERNARY
OPERATORS OPERATORS
OPERATOR
RELATIONAL
CLASSIFICATIO OPERATORS
N OF
OPERATORS LOGICAL
OPERATORS

BASED ON BITWISE
ROLE OF OPERATORS
OPERATOR
ASSIGNMENT
OPERATORS

MISCELLANEOUS
OPERATORS
UNARY 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 operator
3. ! (logical negation)
4. ~ (bitwise negation)
5. ++ (increment operator)
6. -- (decrement operator) etc.
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.
TERNARY OPERATORS
Ternary operator operates on three operands.
Conditional operator (i.e. ?:) is the only ternary
operator available in C.
ARITHMETIC OPERATORS
The arithmetic operations like addition, subtraction, multiplication, division etc. can be
performed by using arithmetic operators. Following arithmetic operators are available in C:

S.No Operator Name of Category ary of Precedence Associativ


Operator Operators amongst ity
arithmetic
class
+ Unary plus Unary Unary Level-I R→L
1.
- Unary minus operators (Highest) (Right-to-left)
++ Increment
-- Decrement
* Multiplication Multiplicative Binary Level-II L→R
2.
/ Division operators (Intermediate) (Left-to-right)
% Modulus
+ Addition Additive Binary Level-III L→R
3.
- Subtraction operators (Lowest)

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 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.
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.
→ 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.
MODES OF BINARY ARITHMETIC
OPERATORS
S.NO. MODE OF OPERATION
1. Integer mode: Here both the operands of a binary arithmetic
operator are of integer type and the result of
operation is also of integer type. E.g.: the result of
4/3 will be 1 instead of 1.3333.
2 Floating point mode Both the operands are of floating point type and the
result is also of floating point type.
e.g.: the result of 4.0/3.0 will be 1.333333.
3 Mixed mode: If one of the operand is of integer type and other is
of floating point type, the mode of operation is said
to be mixed mode. The operand of integer type is
promoted to floating point type and the result will
be of floating point type. For example: the result of
4/3.0 will be 1.333333.
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.
Cont…
 ++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.
I. In case of pre-increment operator, firstly the value of
its operand is incremented and then it is used for the
evaluation of expression.
II. 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.
III. 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.
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.
I. 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.
II. 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.

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.
RELATIONAL OPERATORS
• Relational operators are used to compare two quantities (i.e. their
operands). There are six relational operators in C:

S.No Operator Name of Category ary of Precedence Associativity


Operator Operators amongst
relational
class

1. < Less than Relational Binary Level-I L→R


> Greater than operators
<= Less than or
equal to
>= Greater than or
equal to
2. == Equal to Equality Binary Level-II L→R
!= Not equal to operators
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.
LOGICAL OPERATORS
Logical operators are used to logically relate the expressions. The C language has
the following logical operators:
S.No Operator Name of Category ary of Precedence Associativity
Operator Operators amongst
logical class
1. ! Logical NOT Unary Unary Level-I R→L
2. && Logical AND Logical Binary Level-II L→R
operator
3. || Logical OR Logical Binary Level-III L→R
operator

In C language, there is no operator available for logical eXclusive-OR (XOR)


operation.
KEY POINTS: LOGICAL
OPERATORS
• Logical operators consider operand as an entity, a unit.
• Logical operators operate according to the following truth tables:

OPERAND 1 OPERAND 2 AND OR OPERAND NOT OPERATION


OPERATION OPERATION
FALSE FALSE FALSE FALSE FALSE TRUE

FALSE TRUE FALSE TRUE TRUE FALSE

TRUE FALSE FALSE TRUE

TRUE TRUE TRUE TRUE


Cont…
• 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 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.
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:
S.No Operator Name of Category ary of Precedence Associativity
Operator Operators amongst
bitwise class
1. ~ Bitwise NOT Unary Unary Level-I R→L

2. << Left Shift Shift Binary Level-II L→R


>> Right Shift operators
3. & Bitwise AND Bitwise Binary Level-III L→R
operator
4. ^ Bitwise X- Bitwise Binary Level-IV L→R
OR operator
5. | Bitwise OR Bitwise Binary Level-V L→R
operator
KEY POINTS: BITWISE
OPERATORS
 Bitwise operators operate on the individual bits
of operands and are used for bit manipulation.
 They can only be applied on operands of type
char, short, int, long, whether signed or unsigned.
 The bitwise-AND and bitwise-OR operators
operate on the individual bits of the operands
according to the truth tables specified previously.
 The expression 2&3 evaluates to 2 and 2|3
evaluates to 3.
CONT…
 X-OR operator operates according to the following truth
table:
OPERAND 1 OPERAND 2 XOR OPERATION
False False False

False True True

True False True

True True False

 The bitwise NOT operator results in 1’s complement of its


operand.
 Left shift by one bit is equivalent to multiplication by 2. Left
shift by n bits is equivalent to multiplication by 2n, provided
the magnitude does not overflow.
 Right shift by one bit is equivalent to an integer division by 2.
Right shift by n bits is equivalent to integer division by 2n.
ASSIGNMENT OPERATORS
A variable can be assigned a value by using assignment operator.
The following assignment operators are available in C language:

S.No Operator Name of Operator Category ary of Precedence Associativity


Operators
1. = Simple assignment Assignment Binary Level-I R→L

*= Assign product Shorthand


/= Assign quotient assignment
%= Assign modulus operators
+= Assign sum
-= Assign difference
&= Assign bitwise AND
|= Assign bitwise OR
^= Assign bitwise XOR
<<= Assign left shift
>>= Assign right shift
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 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:
DIFFERENCES
S.No Initialization Assignment
1. First time assignment at the time of Value of a data object after
definition is called initialization. initialization can be changed by the
For example: means of assignment.
int a=10; is initialization of a. 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 Qualified constant cannot be assigned
a value. a value. It is erroneous to write a=10; if
For example, const int a=10; is valid. 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. &)
Conditional operator
Conditional operator is the only ternary operator
available in C.
S.No Operator Name of Category ary of Precedence Associativit
Operator Operators y
1. ?: Conditional Conditional Ternary Level-I R→L
operator

The following are the important points about conditional operator:

 The general form of conditional operator is E1?E2:E3, where E1, E2


and E3 are sub-expressions.
 The sub-expression E1 must be of scalar type .
 The sub-expression E1 is evaluated first. If it evaluates to a non-zero
value (i.e. true), then E2 is evaluated and E3 is ignored.
 If E1 evaluates to zero (i.e. false), then E3 is evaluated and E2 is
ignored.
COMMA OPERATOR
The comma operator is used to join multiple expressions together.

S.No Operator Name of Category ary of Precedence Associativit


Operator Operators y
1. , Comma Comma Binary Level-I L→R
operator

The following are the important points about comma operator:


 Every instance of comma symbol is not a comma operator. The commas separating
arguments in a function call are not comma operators. If commas separating arguments in a
function call are considered as comma operators, then no function could have more than
one argument. The commas used in the declaration/definition statement are not
considered as comma operators. The commas appearing between the arguments in a
function call or commas appearing in a declaration/definition statement are separators.
 The comma operator guarantees left-to-right evaluation.
 In expression, E1, E2, E3…..En, the sub-expression E1, E2, E3….En are evaluated in left-to-
right order. The result and type of evaluation of overall expression is the value and type of
the evaluation of rightmost expression i.e. En.
 Comma operator has least precedence.
sizeof operator:
• The sizeof operator is used to determine the size in bytes that a value or a
data object will take in memory.
S.No Operator Name of Category ary of Precedence Associativit
Operator Operators y
1. sizeof Size-of Unary Unary Level-I R→L
operator

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))
 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 .
ADDRESS-OF OPERATOR
The address-of operator is used to find address i.e. l-value of a data
object.
S.No Operator Name of Category ary of Precedence Associativit
Operator Operators y
1. & Address-of Unary Unary Level-I R→L
operator

The following are the important points about address-of operator:


 The address-of operator must appear towards the left side of its
operand.
 The syntax of using address-of operator is
& operand
 The operand of the address-of operator must be a variable or a
function designator . The address of operator cannot be applied to
constants, expressions, bit-fields and to the variables declared with
register storage class.
COMBINED PRECEDENCE OF ALL
OPERATORS
• Till now we have discussed intra-class precedence , now we will discuss the
inter-class precedence
S.No Operator Name of Category ary of Precedence Associativity
Operator Operators
1. () Function call Level-I
[] Array subscript (Highest)
-> Indirect
. member access
Direct member
access
2. ! Logical NOT Unary Unary Level-II R→L
~ Bitwise NOT
+ Unary plus
- Unary minus
++ Increment
-- Decrement
& Address-of
* Deference
sizeof Sizeof
3. * Multiplication Multiplicative Binary Level-III L→R
/ Division operators
% Modulus
Cont…
S.No Operator Name of Category ary of Precedence Associativity
Operator Operators
4. + Addition Additive Binary Level-IV L→R
- Subtraction operators
5. << Left Shift Shift operators Binary Level-V L→R
>> Right Shift
6. < Less than Relational Binary Level-VI L→R
> Greater than operators
<= Less than or
>= equal to
Greater than or
equal to
7. == Equal to Equality Binary Level-VII L→R
!= Not equal to operators
8. & Bitwise AND Bitwise Binary Level-VIII L→R
operator
9. ^ Bitwise X-OR Bitwise Binary Level-IX L→R
operator
10. | Bitwise OR Bitwise Binary Level-X L→R
operator
11. && Logical AND Logical Binary Level-XI L→R
operator
12. || Logical OR Logical Binary Level-XII L→R
operator
13. ?: Conditional Conditional Ternary Level-XIII R→L
Cont…
S.No Operator Name of Operator Category ary of Precedence Associativity
Operators
14. = Simple assignment Assignment & Binary Level-XIV R→L

*= Assign product Shorthand


/= Assign quotient assignment
%= Assign modulus operators
+= Assign sum
-= Assign difference
&= Assign bitwise AND
|= Assign bitwise OR
^= Assign bitwise XOR
<<= Assign left shift
>>= Assign right shift

15. , Comma operator Comma Binary Level-XV L→R


(Least)
THANK YOU

You might also like