10/24/2011

C Programming – Expressions


C Programming – Expressions

In this tutorial you will learn about Expressin in C programming language - Arithmetic Expressions, Evaluation of Expressions, Precedence in Arithmetic Operators, Rules for evaluation of expression, Type conversions in expressions, Implicit type conversion, Explicit Conversion and Operator precedence and associativity,

Arithmetic Expressions


An expression is a combination of variables constants and operators written according to the syntax of C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of C expressions are shown in the table given below.

Algebraic Expression

C Expression

a x b – 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


 

 

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. All variables used in the expression must be assigned values before evaluation is attempted.




Example of evaluation statements are

x = a * b – c
y = b / c * a
z = a – b / c + d;

The following program illustrates the effect of presence of parenthesis in expressions.


.
main ()
{
float a, b, c x, y, 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 = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}
.

output

x = 10.00
y = 7.00
z = 4.00


 

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 + -


 

 

 

 

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.

 

Type conversions in expressions

Implicit type conversion


C permits mixing of constants and variables of different types in an expression. 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.

The following rules apply during evaluating expressions


All short and char are automatically converted to int then
1. If one operand is long double, the other will be converted to long double and result
.....will be long double.
.
2. If one operand is double, the other will be converted to double and result will be double.
.
3. If one operand is float, the other will be converted to float and result will be float.
.
4. If one of the operand is unsigned long int, the other will be converted into unsigned
.....long int and result will be unsigned long int.
.
5. If one operand is long int and other is unsigned int then
.....a. 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.
.....b. Else Both operands will be converted to unsigned long int and the result will be
..........unsigned long int.
.
6. If one of the operand is long int, the other will be converted to long int and the result will be long int. .
7. If one operand is unsigned int the other will be converted to unsigned int and the
.....result will be unsigned int.


Explicit Conversion


Many times there may arise a situation where we want to force a type conversion in a way that is different from automatic conversion.

Consider for example the calculation of number of female and male students in a class

........................female_students
Ratio =........-------------------
........................male_students

Since if female_students and male_students are declared as integers, the decimal part will be rounded off and its ratio will represent a wrong figure. This problem can be solved by converting locally one of the variables to the floating point as shown below.


Ratio = (float) female_students / male_students

The operator float converts the female_students to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion, the division is performed by floating point mode, thus retaining the fractional part of the result. The process of such a local conversion is known as explicit conversion or casting a value. The general form is

(type_name) expression

Operator precedence and associativity

Each operator in C has a precedence associated with it. The precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of these levels. The operators of higher precedence are evaluated first.

The operators of same precedence are evaluated from right to left or from left to right depending on the level. This is known as associativity property of an operator.


The table given below gives the precedence of each operator.

0 komentar:

Posting Komentar