Table of Contents
Expression Evaluation in C Programming
In this C Programming Tutorial we will discuss about Operators, operands, operator precedence and associativity, Expression and expression evaluation in c programming examples.
Generally expression solving questions are asked in GATE exam. Let’s start with operator introduction.
What is an Operator ?
Operator is a symbol or token in C Programming that tell the compiler to perform specific operation such as mathematical, logical , relational ,conditional and increment and decrement operators.
We explained about different types of operators here.
What is Operand ?
Operand is a variable name which store a specific type of value such as integer, float, double etc.
Operator Precedence and Associativity
Operator precedence determines the order of evaluation of different operators in an expression. Operators are evaluated in higher to lower precedence order.
Associativity of Operator determines the order of evaluation in an expression. Operators are evaluated in higher to lower precedence order. Operators are either left to right associative or right to left associative.
What is an Expression in C ?
In C Programming an expression is a combination of operators and operand. Operator are applied on operand in the expression. Each expression has some specific value as a result which is evaluated after solving the expression.
Operator precedence and associativity of different operators in C is as shown in the following table.
Rules for Expression Evaluation in C
To evaluate the expression in c program we have to follow certain rules. These rules are as follow –
1. If more than one operators are used in an expression then operations are performed on the basis of operator precedence ( priority). Operators are evaluated in the according their precedence order. Operator with higher precedence is evaluated first.
2. If more than one operators with same precedence are present in the expression then these operators are evaluated on the basis of associativity from left to right.
3. Operators with in nested parenthesis are evaluated first.
Expression Evaluation Practice Questions
Problems based on expression evaluation in C programming are discussed in this section.
Rules of expression evaluation in c programming are applied to solve each question.
Q1. Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions:
I. a + b – c + d
II. a * b/c+d
III. 1 + a * b % c
IV. a+d %b-c
Answer : (i) 4 (ii) 4 (iii) 3 (iv) – 2
Q2. If int a=1, b=2, c=3 then what is the value of the following expression
++a * b – c – –
Answer : 1
Q3. If int a=1,b=2,c=3, d= 4 then what is the value of the following expression.
++b / c + a * d++
Answer : 5
Q4: what will be the value of this expression 6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)
Answer : 17
Solution
Step 1 – 6*2/( 2+1 * 2/3 +6) +8 * (8/4)
Step 2- 6*2/(2+2/3 + 6) + 8 * (8/4)
Step3- 6*2/(2+0+6) + 8 * (8/4)
Step 4- 6*2/ 8+ 8 * (8/4)
Step 5- 6*2/8 + 8 * 2
Step 6 – 12/8 +8 * 2
Step 7- 1 + 8 * 2
Step 8 -1 + 16
Final Value – 17
Q5: if int a=8,b=15,c=4 then what will the value of the given expression
2*((a%5)*(4+(b-3)/( c+2)))
Answer : 36
Q6: if int a=5 then what will be value of x
X= 17- 2/4 * 2 + 3 – ++a
Answer: 14
Q7.If int a=9,b=12,c=3 then what will be value of x X=a- ((b/(3+c)) *2) -1
Answer: 4
Q8. If a,b,c,d and e are of integer types and a=11,b=6,c=0,d=7,e=5
What will be the value of this expression
a+2 > b &&!c ||a !=d && a-2<=e
Answer : 1
Conclusion and Summary
In this C Programming Tutorial we have discussed expression evaluation in c programming with examples and rules. We have also discussed the concept of operator precedence and associativity in c programming.