Unit - 1 CP (Lecture 7)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

University Institute Of Computing

Bachelor of Computer Applications


Subject Name: Computer Programming
Code:20CAT111

1
Topics to be Covered

• Expressions
• Evaluation of expressions
• Type conversion
• precedence
• associatively

Fig. 7.1 operators and expressions [1] 2


Expressions

• An expression is a combination of operators, constants, variables and operands. An


expression may consist of one or more operands, and zero or more operators to
produce a value.
• Example:
C=a+b;

Fig. 7.2 Expression [2]


3
Example of C Expressions

axb–c a*b–c

(m + n) (x + y) (m + n) * (x + y)

(ab / c) a*b/c

3x2 +2x + 1 3*x*x+2*x+1

(x / y) + c x/y+c

Table. 7.1(example of C Expressions)


4
Types of Expressions

Fig. 7.3 Types of expressions [3]


5
Evaluation of Expressions

• Expressions are evaluated using an assignment statement of the form


Variable = expression;
• Variable is any valid C variable name. When the statement is encountered, the
expression is evaluated first and then replaces the previous value of the variable on the
left hand side.
• Example of evaluation statements are: where, values of a, b and c are –
a=30, b=20, c=10
x = a * b – c =30*20-10 = 200-30 =170
y=b/c*a
z = a – b / c + d;

6
Precedence in Arithmetic Operators

• An arithmetic expression without parenthesis will be evaluated from left to right using
the rules of precedence of operators. There are two distinct priority levels of arithmetic
operators in C.

High priority * / %
Low priority + -

7
Rules for evaluation of expression

• First parenthesized sub expression left to right are evaluated.


• If parenthesis are nested, the evaluation begins with the innermost sub expression.
• The precedence rule is applied in determining the order of application of operators in
evaluating sub expressions.
• The associability rule is applied when two or more operators of the same precedence
level appear in the sub expression.
• Arithmetic expressions are evaluated from left to right using the rules of precedence.
• When Parenthesis are used, the expressions within parenthesis assume highest priority.

8
Type Conversions

• A type cast is basically a conversion from one type to another.


• It may so happen that the type of the expression and the type of the variable on the
left-hand side of the assignment operator may not be same.
• In such a case the value of the expression is promoted or demoted depending on the
type of the variable on left-hand side of =.
• C allows for conversions between the basic types, implicitly or explicitly

9
Implicit Type Conversion

• C automatically converts any intermediate values to the proper type so that the
expression can be evaluated without loosing any significance. This automatic type
conversion is know as implicit type conversion
• During evaluation it adheres to very strict rules and type conversion. If the operands
are of different types the lower type is automatically converted to the higher type
before the operation proceeds. The result is of higher type.
• Type conversions can be implicit which is performed by the compiler automatically, or
it can be specified explicitly through the use of the cast operator.

10
Implicit Conversion

(a) An arithmetic operation between an integer and integer always yields an integer
result.
(b) An operation between a real and real always yields a real result.
(c) An operation between an integer and real always yields a real result. In this operation
the integer is first promoted to a real and then the operation is performed. Hence the
result is real.

Table 7.2 Implicit Conversion [4]


11
Implicit conversion

• Conversion during assignments:


char c='a';
int i;
i=c;
/* i is assigned the ASCII code of ‘a’ */

Fig 7.4 – Implicit Type Conversion [5]


12
Type Casting/Explicit Type Casting

• A cast, or explicit type conversion, is special programming instruction which specifies


what data type to treat a variable as (or an intermediate calculation result) in a given
expression.

13
Operator Precedence And Associativity

Table 7.3 - Operator precedence and associativity


14
Operator Precedence And Associativity

Table 7.4 - Operator precedence and associativity


15
Operator Precedence And Associativity

Table 7.5- Operator precedence and associativity


16
References

• [1] https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQ1BaB25ZbSO5Oq2uvpkrYA8XxZWI95l
• colqeG8ONh4ZX0J-_D&usqp=CAU
• [2] https://media.geeksforgeeks.org/wp-content/uploads /20190801163131/What-is-an-Expression_-3.jpg
• [3] https://media.geeksforgeeks.org/wp-content/uploads/20190801154515/Types-of-Expressions.jpg
• [4] https://www.thecrazyprogrammer.com/type-conversion-precedence-and-associativity-of-operators-in-
c.html
• [5] Ref. -https://data-flair.training/blogs/wp-content/uploads/sites/2/2019/06/Implicit-Type-Conversion-.jpg

17
Reference Books
• T1 E. Balaguruswamy, Programming in ANSI, McGraw-Hill Education,
• https://bookmart.online/shop/programming-in-ansi-c-8-e-balagurusamy-second-hand-book/?ut
m_source=Google%20Shopping&utm_campaign=test&utm_medium=cpc&utm_term=498

• R1 Byron S. Gottfried, Programming with C, McGraw-Hill (2nd Edition)


• https://www.amazon.com/Schaums-Outline-Programming-Byron-Gottfried/dp/0070240353/ref
=sr_1_1?dchild=1&keywords=%E2%80%A2+Programming+with+C%2C+Gottfried%2C+a
nd+McGraw-Hill.&qid=1593001561&s=books&sr=1-1

• R2 Brian W. Kernighan, Dennis M. Ritchie, The ANSI C Programming Language, Prentice-


Hall 2nd Edition.
• http://www2.cs.uregina.ca/~hilder/cs833/Other%20Reference%20Materials/The%20C
%20Programming%20Language.pdf

18
THANK YOU
For queries
Email:[email protected]

You might also like