Arithmetic Expression in C With Example

In this post, we will learn about Arithmetic Expression in c. We will go through what is an arithmetic expression? How to evaluate C arithmetic expressions?, What is precedence or associativity of arithmetic operators? Why precedence and associativity is important to evaluate any expression in c. We will discuss all of these with appropriate examples.

Prerequisite:
You must have knowledge of Constant , Variable and Operators in c programming

1. Arithmetic Expressions

An arithmetic expression in c is a combination of variables, constants, and operators arranged as per the syntax of the language.

C can handle any complex mathematical expressions.

Examples of C expressions:

Algebraic Expression C Expression
a x b – c a * b – c
(m + n)(x + y) (m + n) * (x + y)
ab / c (a*b) / c
3x + 2x + 1 (3 * x * x) + (2 * x) + 1
x / y + c x / y + c

1.1 Evaluation of Arithmetic Expression in c

Expressions are evaluated using an assignment statement of the form:

 
variable = expression;
  • Variable is any valid C variable name.
  • When the statement is encountered, the expression is evaluated first and the result then replaces the previous value of the variable(on the left-hand-side).
  • All variables used in the expression must be assigned values before evaluation is attempted.

Examples of Evaluation Statement:

 
x = a * b - c;
y = b / c * a;
z = a - b / c + d;
  • When these statements are used in a program, the variables x, y, z, a, b, c and d must be defined before used in the expressions.
  • The blank space around an operator is optional and adds only to improve readability.

Program to illustrate the concept of evaluation of expressions:

#include<stdio.h>
int main()
{
	// Declaration of Variables
	float a, b, c, x, y, z; 

	// Assigning Values to variables a, b, c
	a = 10;
	b = 12;
	c = 2;

	// Expressions
	x = a - b / 3 + c * 2 - 1;        
	y = a - b / (3 + c ) * (2 - 1);
	z = a - (b / (3 + c) * 2) - 1;

	// Print values of x , y , z after expression evaluation  
	printf("x = %f\n", x);
	printf("y =%f\n", y);
	printf("z =%f\n", z);

	return 0;
}

Output:

arithmetic expression in c with example

1.2 Precedence of Arithmetic Operators in C

Each arithmetic operator in C has a precedence associated with it. It is used to determine how an expression involving more than one operator is evaluated.

Operators Operations Order of Evaluation(Precedence)
( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e. ,not nested), they are evaluated left to right.
* / % Multiplication, Division, Remainder Evaluated second. If there are several, they are evaluated left to right
+ – Addition, Substraction Evaluated third. If there are several, they are evaluated left to right
= Assignment Evaluated last
Example 1

Consider following statements:
x = a – b / 3 + c * 2 – 1;

Assume a = 10, b = 12, c = 2, the statement becomes
x = 10 – 12 / 3 + 2 * 2 – 1 is evaluated is as follows

Step 1 : x = 10 - 4 + 2 * 2 -1    (Division is Evaluated 12 / 3 = 4)
Step 2 : x = 10 - 4 + 4 -1        (Multiplication is Evaluated 2 * 2 = 4)
Step 3: x = 6 + 4 -1              (Substraction is Evaluated 10 - 4 = 6)
Step 4: x = 10 -1                 (Addition is Evaluated 6 + 4 = 10)
Step 5: x = 9                     (Substraction is Evaluated 10 - 1 = 9 and Assign 9 to x)
Example 2

Consider the following Statements:
y = a – b / ( 3 + c) * (2 -1 );
when a = 10, b = 12 and c = 3
y = 10 – 12 / (3 + 3 ) * (2 -1) is evaluated as follows:

Step 1 : y = 10 - 12 / 6 * ( 2 -1)
Step 2 : y = 10 - 12 / 6 * 1
Step 3: y = 10 - 2 * 1
Step 4: y = 10 - 2
Step 5: y = 8

This example contains three left-to-right passes, while the number of evaluation steps remain the same as 5 ( i.e. equal to the number of arithmetic operators).

Parentheses may be nested, in that cases, evaluation of the expression will proceed outward from the innermost set of parentheses and make sure that every opening parenthesis has a matching closing one.

For Example:

 
Expression 1: 9 - (12 / ( 3 + 3) * 2 ) - 1 = 4

Expression 2: 9 - ((12 / 3) + 3 * 2  ) - 1 = -2

1.3 Associativity

  • The operators of the same precedence are evaluated either from left to right or from right to left, depending on the level. This is known as Associativity property of an operator.
  • It is used when two operators of same precedence appear in an expression.
Description Operator Associativity
Parentheses or Function Call ( )  Left to Right
Brackets or Array Subscript  [ ]  Left to  Right
Dot or Member Selector Operator .   Left to  Right
Arrow Operator  ->  Left to  Right
Postfix Increment/ Decrement  ++ , —  Left to  Right
Prefix Increment/ Decrement   ++ —   Right To Left
Unary Plus   +   Right To Left
Unary Minus  –  Right To Left
Logical Negation  !  Right To Left
Bitwise Complement  ~  Right To Left
Type Cast  (type)  Right To Left
Dereferencing Operator  *  Right To Left
Address of Operator  &  Right To Left
Determines  size in Bytes  sizeof()  Right To Left
Multiplication  *  Left to  Right
Division  /  Left to  Right
 Modulus   %    Left to  Right
 Addition   +    Left to  Right
Substraction   –    Left to  Right
Bitwise  Left Shift <<  Left to  Right
Bitwise  Right Shift  >>  Left to  Right
Relational Less than  <  Left to  Right
Relational Less than equal to  <=    Left to  Right
Relational Greater than  >    Left to  Right
Relational Greater than equal to  > =    Left to  Right
Realtional Equal  = =    Left to  Right
Realtional  not Equal to  ! =    Left to  Right
Bitwise AND  &    Left to  Right
Bitwise Exclusive OR  ^    Left to  Right
Bitwise Inclusive OR  |    Left to  Right
Logical AND  &&    Left to  Right
Logical OR  ||    Left to  Right
Ternary Operator  ? :    Right to Left
Assignment  =    Right to Left
Addition Assignment  + =    Right to Left
Substraction Assignment  – =    Right to Left
Multiplication Assignment  * =    Right to Left
Division Assignment  / =    Right to Left
Modulus Assignment  % =    Right to Left
Bitwise  AND Assignment  & =    Right to Left
Bitwise Exclusive OR  ^=    Right to Left
Bitwise Inclusive OR  |=    Right to Left
Bitwise  Left Shift  <<=  Right to Left
Bitwise  Right Shift  >>=    Right to Left
Comma Operator  ,  Left to Right
Example 1

ASSUME x = 25 and y = 20

Consider the following statement:
z = (x == 10 + 15 && y < 10)

This statement have assignment operator(=), relational is equal to operator(==), arithmetic addition operator(+), logical AND operator(&&) and Relational less than operator(<). Arithmetic Addition has highest priority than the remaining operators in statement. So,

  • At first, Addition of 10 and 15 is executed.
    z = (x = = 25 && y < 10)
  • In second step, < operator has highest priority than the remaining operators in given statement. Now, z = ( x = = 25 && FALSE)
  • In third step, == operator has highest priority than the remaining operators in given statement.
    Now, z = ( TRUE && FALSE)
  • In fourth step, && operator has highest priority than assignment operator.
    Now, z = 0, so, result 0 is assigned to variable z because associativity of assignment operator is right to left.
Example 2

Assume x = 10 , y = 20
z = x * y / x;

In this statement, * and / operators have same precedence.
Now, we will evaluate this statement according to associativity rule , * and / is left to right associative.
Step 1: z = 10 * 20 / 10;
Step 2: z = 200 / 10;
Step 3: z = 20;

Reference

C Expression Rules

Thus, that’s end of tutorial on arithmetic expression in c with example.

Leave a Reply