Sunteți pe pagina 1din 15

EXPRESSIONS

Objectives
After this Session, you will be able to Identify expressions Understand the precedence of arithmetic operators Know how type conversion works Get knowledge about mathematical functions of C

EXPRESSIONS
The combination of operators and operands is said to be an expression. ARITHMETIC EXPRESSIONS An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. Eg 1) a = x + y; EVALUATION OF EXPRESSIONS Expressions are evaluated using an assignment statement of the form variable = expression; Eg:1) x = a * b c; 2) y = b / c * a;

PRECEDENCE OF ARITHMETIC OPERATORS


An arithmetic expression without parenthesis will be evaluated from left to right using the rule of precedence of operators. There are two distinct priority levels of arithmetic operators in C.
High priority * / % Low priority + -

Program
/*Evaluation of expressions*/

main() { float a, b, c, y, x, z; a = 9; b = 12; c = 3; x = a b / 3 + c * 2 1; y = a b / (3 + c) * (2 1); z = a (b / (3 + c) * 2) 1; printf(x = %f \n,x); printf(y = %f \n,y); printf(z = %f \n,z); }

OUTPUT
x = 10.000000 y = 7.000000 z = 4.000000

TYPE CONVERSION IN EXPRESSIONS


Automatic Type Conversion C permits the mixing of constants and variables of different types in an expression. If the operands are of different types, the lower type is automatically converted to higher type before the operation proceeds. The result is of the higher type. Next slide has sequence of rules that are applied while evaluating expressions

Rules for evaluating expressions


All short and char are automatically converted to int; then If one of the operands is long double, the other will be converted to long double and the result will be in long double. Else, If one of the operands is double, the other will be converted to double and the result will be in double. Else, If one of the operands is float, the other will be converted to float and the result will be in float.

Rules for evaluating expressions (Contd)


Else, if one of the operands is long int and other is unsigned int, then: (a) if unsigned int can be converted to long int, the

unsigned int operand will be converted as such and the result will be long int. (b) else, both the operands will be converted to unsigned long int and the result will be unsigned long int.

Else, If one of the operands is long int, other will be converted to long int and result will be in long int. Else, If one of the operands is unsigned int, other will be converted to unsigned int and result will be in unsigned int.

the the the the

Casting a Value
C performs type conversion automatically. However, there are instances when we want to force a type conversion in a way that is different from the automatic conversion. Eg: 1) ratio = Total_women / Total_men Since Total_women and Total_men are declared as integers the ratio would represent a wrong figure. Hence it should be converted to float. ratio = (float) Total_women / Total_men The general form of a cast is: (type-name)expression

MATHEMATICAL FUNCTIONS
Mathematical functions such as sqrt, cos, log etc., are the most frequently used ones. To use the mathematical functions in a C program, we should include the line #include<math.h> in the beginning of the program.

MATHEMATICAL FUNCTIONS

MATHEMATICAL FUNCTIONS

MATHEMATICAL FUNCTIONS

Check Your Progress


Ex 1) What will be the output of the expressions given below: Given a = 5, b=2 x=a+b*2 y= a*b+2 ---------------------------------------- -----------------------------------------

S-ar putea să vă placă și