My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on Expression Evaluation in C

An expression is a sequence of operands and operators that reduces to a single value. For example, the expression, 10+5 reduces to the value of 15. Based on the operators and operators used in the expression, they are divided into several types. Some of them are:

  1. Integer expressions – expressions which contains integers and operators
  2. Real expressions – expressions which contains floating point values and operators
  3. Arithmetic expressions – expressions which contain operands and arithmetic operators
  4. Mixed mode arithmetic expressions – expressions which contain both integer and real operands
  5. Relational expressions – expressions which contain relational operators and operands
  6. Logical expressions – expressions which contain logical operators and operands
  7. Assignment expressions and so on… – expressions which contain assignment operators and operands

 

Expression Evaluation

Expressions are evaluated using an assignment statement of the form:

expression

In the above syntax, variable is any valid C variable name. When the statement like the above form is encountered, the expression is evaluated first and then the value is assigned to the variable on the left hand side. All variables used in the expression must be declared and assigned values before evaluation is attempted. Examples of expressions are:

expression-examples

Expressions are evaluated based on operator precedence and associativity rules when an expression contains more than one operator.

 

Operator Precedence and Associativity

Every C operator has a precedence (priority) associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are different levels of operator precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators in the same level of precedence are evaluated from left to right or from right to left, based on the associativity property of an operator. In the below table we can look at the precedence levels of operators and also the associativity of the operators within the same level. Rank 0 indicates the lowest precedence and Rank 14 indicates highest precedence.

The precedence and associativity of various operators in C are as given below:

operator-precedence-c

Example 1

Let’s understand the operator precedence and associativity rules with the help of an example. The expression that we consider for this example is:

if(x==10+15&&y<10)

In the above expression, the operators used are: ==,+,&& and <. By looking at the operator precedence table, + operator is at level 4, < operator is at level 6, == operator is at level 7 and && operator is at level 11. So clearly, the + operation is performed first. Then our expression becomes:

if(x==25&&y<10)

Now, since < operator has the next highest precedence, y<10 is evaluated. If we assume value of x is 20 and value of y is 5, then the value of y<10 is true. Then the == operator is evaluated. Since value of x is 20, x==25 evaluates to false. So, our expression becomes:

if(false&&true)

Now, the only operator left is &&. It is evaluated next and the result is false.

Example 2

Let us try to evaluate an arithmetic expression as shown below:

x = a-b/3+c*2-1

Let a = 9, b =12, and c=3. Then our expression becomes:

x = 9-12/3+3*2-1

From the above table, we can see that the * and / operators are having higher precedence than + and – operators. Also, the * and / operators are at the same level of precedence, so we have to apply the associativity rules. Since the associativity rule is left-to-right, we apply the / operator first and the expression evaluates to:

x = 9-4+3*2-1

Next, we apply the * operator and the expression becomes:

x = 9-4+6-1

Next, we apply the first – operator as the – and + operators are at the same level and the associativity rule is from left to right. The expression becomes:

x = 5+6-1

Now, we apply the + operator and the expression become:

x = 11-1

Finally, we apply the – operator and the result is:

x = 10

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me