Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. 2 Comments on Expression Evaluation in C with Examples

In this article we will learn about expression evaluation in C with examples. First let’s understand what is an expression and how the expressions are evaluated in a C program.

 

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.

 

Watch this video to learn about expressions in C programming language:

 

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 evaluation in c with examples

 

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.

 

Watch this video to learn about operator precedence and associativity in C programming:

 

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

 

Watch this video to learn expression evaluation in C programming with the help of an example:

 

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

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

What is single ‘&’ use sir? Example 2&3

    The single ‘&’ is used to perform bit-wise AND operation. So, the result of 2&3 is 2.

Leave a Reply

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