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:
- Integer expressions – expressions which contains integers and operators
- Real expressions – expressions which contains floating point values and operators
- Arithmetic expressions – expressions which contain operands and arithmetic operators
- Mixed mode arithmetic expressions – expressions which contain both integer and real operands
- Relational expressions – expressions which contain relational operators and operands
- Logical expressions – expressions which contain logical operators and operands
- Assignment expressions and so on… – expressions which contain assignment operators and operands
Expressions are evaluated using an assignment statement of the form:
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:
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:
Let’s understand the operator precedence and associativity rules with the help of an example. The expression that we consider for this example is:
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:
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:
Now, the only operator left is &&. It is evaluated next and the result is false.
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