## Introduction to Expression Evaluation in C

While knowing about expression evaluation we must understand what is an expression in C and what is an expression means. An expression in C is defined as 2 or more operands are connected by one operator and which can also be said to a formula to perform any operation. An operand is a function reference, an array element, a variable, or any constant. An operator is symbols like “+”, “-“, “/”, “*” etc. Now expression evaluation is nothing but operator precedence and associativity. Expression precedence in C tells you which operator is performed first, next, and so on in an expression with more than one operator with different precedence. This plays a crucial role while we are performing day to day arithmetic operations. If we get 2 same precedences appear in an expression, then it is said to be “Associativity”. Now in this case we can calculate this statement either from Left to right or right to left because this both are having the same precedence.

### Types of Expression Evaluation in C

In C there are 4 types of expressions evaluations

- Arithmetic expressions evaluation
- Relational expressions evaluation
- Logical expressions evaluation
- Conditional expressions evaluation

Every expression evaluation of these 4 types takes certain types of operands and used a specific type of operators. The result of this expression evaluation operation produces a specific value.

### Example to Implement Expression Evaluation in C

Below are some examples mentioned:

#### 1. Arithmetic expression Evaluation

Addition (+), Subtraction(-), Multiplication(*), Division(/), Modulus(%), Increment(++) and Decrement(–) operators are said to “Arithmetic expressions”. These operators work in between operands. like A+B, A-B, A–, A++ etc. While we perform the operation with these operators based on specified precedence order as like below image.

`A+B*C/D-E%F`

Arithmetic Expression evaluation order:

**Code: **

`//used to include basic C libraries`

#include <stdio.h>

//main method for run c application

int main()

{

//declaring variables

int a,b,result_art_eval;

//Asking the user to enter 2 numbers

printf("Enter 2 numbers for Arithmetic evaluation operation\n");

//Storing 2 numbers in varaibles a and b

scanf("%d\n%d",&a,&b);

//Arithmetic evaluation operations and its result displaying

result_art_eval = a+b*a/b-a%b;

printf("================ARITHMETIC EXPRESSION EVALUATION==============\n");

printf("Arithmetic expression evaluation of %d and %d is = %d \n",a,b,result_art_eval);

printf("==============================================================");

return 0;

}

**Output:**

**Explanation: **As you can see in the above example arithmetic expression values evaluated based on precedence as the first *, followed by /, %, + and -.

#### 2. Relational expression evaluation

== (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to) operators are said to “Relational expressions”. This operator works in between operands. Used for comparing purpose. Like A==B, A!=B, A>B, A<B etc. While we perform the operation with these operators based on specified precedence order as like the below image.

4.5 (4,660 ratings)

View Course

`A==B || A!=B||A<B||A>B;`

Relation expression evaluation order:

**Code: **

`//used to include basic C libraries`

#include <stdio.h>

//main method for run c application

int main()

{

//declaring variables

int a,b;

int result_rel_eval;

//Asking the user to enter 2 numbers

printf("Enter 2 numbers for Relational evaluation operation\n");

//Storing 2 numbers in varaibles a and b

scanf("%d\n%d",&a,&b);

//Relational evaluation operations and its result displaying

//If we get result as 1 means the value is true and if it is 0 then value is false in C

result_rel_eval = (a<b||a<=b||a>=b||a>b||a!=b);

printf("================RELATIONAL EXPRESSION EVALUATION==============\n");

if(result_rel_eval==1)

{

printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);

}

else

{

printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);

}

printf("===============================================================");

return 0;

}

**Output:**

**Explanation: **As you can see in the above example relational expression values evaluated based on precedence as First <, followed by <=, >, >=, ==, !=.

#### 3. Logical expression evaluation

&&(Logical and), ||(Logical or) and !(Logical not) operators are said to “Logical expressions”. Used to perform a logical operation. These operators work in between operands. Like A&&B, A||B, A!B etc. While we perform the operation with these operators based on specified precedence order as like the below image.

`A&&B||B!A;`

Logical expression evaluation order:

**Code: **

`//used to include basic C libraries`

#include <stdio.h>

//main method for run c application

int main()

{

//declaring variables

int a,b;

int result_log_eval;

//Asking the user to enter 2 numbers

printf("Enter 2 numbers for Logical evaluation operation\n");

//Storing 2 numbers in varaibles a and b

scanf("%d\n%d",&a,&b);

//Logical evaluation operations and its result displaying

//If we get result as 1 means the value is true and if it is 0 then value is false in C

result_log_eval = (a||b&&a);

printf("================LOGICAL EXPRESSION EVALUATION==============\n");

if(result_log_eval==1)

{

printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);

}

else

{

printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);

}

printf("===============================================================");

return 0;

}

**Output:**

**Explanation: **As you can see in the above example logical expression values evaluated based on precedence as the First &&, followed by || and !.

#### 4. Conditional expression Evaluation

?(Question mark) and :(colon) are said to “Conditional expressions”. Used to perform a conditional check. It has 3 expressions first expression is condition. If it is true then execute expression2 and if it is false then execute expression3. Like (A>B)?”A is Big”:”B is Big”. While we perform the operation with these operators based on specified precedence order as like the below image.

`(X+2=10)?'true':'false';`

Conditional expression Evaluation order:

**Code: **

`//used to include basic C libraries`

#include <stdio.h>

//main method for run c application

int main()

{

//declaring variables

int a,b,c;

int result_con_eval;

//Asking the user to enter 3 numbers

printf("Enter 3 numbers for Conditional evaluation operation\n");

//Storing 3 numbers in varaibles a, b and c

scanf("%d\n%d\n%d",&a,&b,&c);

//Conditional evaluation operations and its result displaying

result_con_eval=(a>b && a>b)? a: (b>a && b>c)? b:c;

printf("================CONDITIONAL EXPRESSION EVALUATION==============\n");

printf("Maximum value of %d, %d and %d is = %d \n",a,b,c,result_con_eval);

printf("===============================================================");

return 0;

}

**Output:**

**Explanation: **As you can see in the above example conditional expression values evaluated based on precedence as First ? and:.

### Conclusion

Expression evaluation in C is used to determine the order of the operators to calculate the accurate output. Arithmetic, Relational, Logical, and Conditional are expression evaluations in C.

### Recommended Articles

This is a guide to Expression Evaluation in C. Here we discuss an introduction to Expression Evaluation in C, with types and respective examples for better understanding. You can also go through our other related articles to learn more –