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

C Programming - Expressions Types

C programming expressions can combine variables, constants, and operators. Expressions are evaluated from left to right based on operator precedence, with multiplication and division having higher precedence than addition and subtraction. Parentheses can be used to alter the order of evaluation. Implicit type conversions may occur during expression evaluation to accommodate different data types, while explicit conversions can be used to cast variables to a different type.

Uploaded by

Jazz Virak
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

C Programming - Expressions Types

C programming expressions can combine variables, constants, and operators. Expressions are evaluated from left to right based on operator precedence, with multiplication and division having higher precedence than addition and subtraction. Parentheses can be used to alter the order of evaluation. Implicit type conversions may occur during expression evaluation to accommodate different data types, while explicit conversions can be used to cast variables to a different type.

Uploaded by

Jazz Virak
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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 C Expression
Expression
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

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.

Order Category Operator Operation Associativity

1 Highest () Function call L→R


precedence [] Left to Right

::
.

2 Unary ! Logical negation (NOT) R→L


~ Bitwise 1’s complement Right -> Left
+ Unary plus
- Unary minus
++ Pre or post increment
-- Pre or post decrement
& Address
* Indirection
Size of Size of operant in bytes

3 Member .* Dereference L→R


Access →* Dereference

4 Multiplication * Multiply L→R


/ Divide
% Modulus

5 Additive + Binary Plus L→R


- Binary Minus

6 Shift << Shift Left L→R


>> Shift Right

7 Relational < Less than L→R


<= Less than or equal to
> Greater than
>= Greater than or equal to

8 Equality == Equal to L→R


!= Not Equal to

9 Bitwise AAND & Bitwise AND L→R

10 Bitwise XOR ^ Bitwise XOR L→R

11 Bitwise OR | Bitwise OR L→R

12 Logical AND && Logical AND L→R

14 Conditional ?: Ternary Operator R→L


15 Assignment = Assignment R→L
*= Assign product
%= Assign reminder
/= Assign quotient
+= Assign sum
-= Assign difference
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift

You might also like