What are Arithmetic Expressions in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Arithmetic expressions are a combination of operands interjoined with arithmetic operators to ultimately evaluate to a single value. We humans use a technique called BODMAS to evaluate arithmetic expressions in real life, but computers and the C programming language use a similar but different technique: precedence and associativity.

Arithmetic Expressions in C

Most people, when hearing the word expressions, they think of equations. Some of you may be asking yourself the question, “Aren't they the same?”. Let me clear that up for you.

You may have found yourself in this situation one or more times. You are standing at the canteen of your college or School or expressions. Someone walks up next to and asks for a particular product, say like Maggi to the shopkeeper. The shopkeeper finds that Maggi is not available, and he would just give some other instant noodles, and the buyer would just take it and pay the cash. That's it. No questions asked. 🤨

What happens during those situations is that the buyer actually meant Instant Noodles and not exactly Nestle's Maggi. The name Maggi was so profound that it replaced the name 'Instant Noodles'.

The same goes with equations and expressions, when people hear the word expression, their mind usually thinks about equations and not expressions. One thing to understand and to make note of is that an expression is a vital part of an equation, but not the only one.

To continue further down this road, we need to learn some Jargon. Fear not, these are just simple terms, and it is highly likely that you already know them.

Operand

A variable or a constant over which the operation has to be performed

Operator

The symbol denotes what operation is to be performed.

Expression

A combination of operands interjoined with operators, that make logical sense.

Equation

An equation is essential of three parts, a variable, assignment operator -- equal to, and an expression.

Types of Expressions in C

Now that you know what an expression is, let's continue with the different types of expression that are in the C programming language. There are four kinds of expressions in the C programming language.

They are

  • Arithmetic expressions
  • Relational expressions
  • Logical expressions
  • Conditional expressions

These expressions are categorized by the inputs they take and the output they produce. In this article, we will be focusing on Arithmetic expressions. The Arithmetic expressions of C are similar to that of most other programming languages.

What is an Arithmetic Expression in C?

An Arithmetic Expression is a combination of operands and Arithmetic operators, such as addition, subtraction, and so on. These combinations of operands and operators should be mathematically meaningful, otherwise, they can not be considered as an Arithmetic expression in C.

The below table lists the different arithmetic operators available in the C programming language, along with a small description.

SymbolUnary / BinaryDescription
+unarydenotes that the number is a positive integer.
-Unarydenotes that the number is a negative integer.
++UnaryIncrements the value of the variable by 1
--UnaryDecreases the value of the variable by 1
+Binaryperforms the mathematical addition of the two given operands.
-Binaryperforms the mathematical subtraction of the two given operands.
*Binaryperforms the mathematical multiplication of the two given operands.
\Binaryperforms the mathematical division of the two given operands and returns the quotient.
%Binaryperforms the mathematical division of the two given operands and returns the remainder as the result.

The below table consists of some valid and invalid arithmetic expressions in C, with the explanation for all the invalid expressions.

Arithmetic ExpressionValid or InvalidExplanation
6.4%5.0Invalidmodulo operator only works with integer values.
a-InvalidIf the minus is a unary operator it should be before the operand, If the minus is a binary operator, then the second operand is missing. So, either way, the Expression is invalid.
-aValidThe unary minus operator is followed by an operand
a-bValidThe binary minus operator has two valid operands, so this is a valid Arithmetic Expression.
5%2ValidThe modulo operator has two integer operands.

Evaluation Of Arithmetic Expressions in C

The evaluation of an arithmetic expression is based on three different things; precedence, the associativity of the arithmetic operators, and the data types of the operands over which the arithmetic operation is being performed.
This thing is more like the BODMAS rule that you learned during your school days but can feel a little different. So let's first start with the two unfamiliar terms and try to understand them.

Precedence

An Arithmetic expression can have more than one arithmetic operator within it. Then how can you determine in which order will you execute them? Let us say that you have an arithmetic expression to evaluate such as 2+3*5, so if you perform the multiplication first you will get an answer as 17, and if you perform the addition first and then perform multiplication, you will get an answer as 25. So, I hope, you get the need for a universally accepted order of priority when it comes to executing different arithmetic operators in an expression. Because a different order of execution of the operators would give us a different result.

Below is the table containing the list of precedence of all the arithmetic operators in the C programming language.

PrecedenceOperator
0parenthesis ()
1Unary plus (+), Unary minus (-), Increment (++), Decrement (--)
2multiplication (*), division (/), modulo (%)
3addition (+), Subtraction (-)

The above table only lists the precedence of the Arithmetic operators, it is to be noted that there are also, other operators in the C programming language. It is up to the reader to learn about the other operators and their precedence if he wishes to.

The operator with the lowest precedence gets executed first, when an expression has more than one operator of the same precedence, then the order in which the operation is based on the associativity of the operators involved in the Expression.

Associativity

The Associativity property of the C programming language, states the direction in which the operation will be performed. Apart from specifying the direction of the operation that has to be performed, the Associativity property also helps us solve the problem of which operation to perform when two operations have the same precedence.

The below table shows the Associativity of all the arithmetic operators.

OperatorAssociativity
Unary Plus (+)Right to Left
Unary Minus (-)Right to Left
Increment (++)Depends on the usage
Decrement (--)Depends on the usage
Addition (+)Left to Right
Subtraction (-)Left to Right
Multiplication (*)Left to Right
Division (/)Left to Right
Modulo (%)Left to Right

In the case of the Increment and Decrement operator, the associativity of the operator depends on its usage. The associativity of the operators is Left to Right in case of prefix operation, and Right to Left operator in case of postfix operator.

I have mentioned that the Associativity property is used to determine which operation to be performed when two or more operators have the same precedence. Now, let us look at an example that demonstrates the above-mentioned scenario.

Consider the expression: -++a*2, where a = 5.

In this expression, both the unary minus (-), and increment operator (++), have the same precedence, so which operation should we perform first. The Associativity of the unary minus is from Right to Left, and the Right-Hand side of the operator has an expression. Hence, we have to evaluate the expression.

The expression to the Right of the unary minus (-) is ++a*2, the value of an is 5, so the expression becomes ++5*2, the increment operator has higher precedence when compared with the multiplication operator. So the compiler will perform the increment operation and replace it in the expression with the result, i.e., 6.

Now back to the original expression, the expression is -6*2, still the right-hand side of the unary minus is an expression, so the compiler will evaluate the multiplication first before performing the unary minus. So after the multiplication the expression is -12, It is now the unary minus is evaluated, and the final answer is -12.

Output

Apart from these two things, the data types of the operands involved also impact the result of an Arithmetic Expression in the C programming language.

Data types

The Data types of the operands also have an impact on the result of the Arithmetic expression. As you might have noticed most of the Arithmetic operators are binary operators and need operands to perform their functionality.

What do you think will be the result of this expression: 5/2.5?

You might think that the result will be 2, but unfortunately, it is not the case. When you evaluate the expression 5/2.5 with a standard C compiler, you will get 2.0 as the result. Let me explain the reason WHY.

The arithmetic operator always chooses the data types that are more efficient to store the resultant value of the Arithmetic Expression. Most of the Arithmetic operators are binary operators, and there can be three cases.

  • Both the operands are of integer data type. Then the result will also be of integer data type.
  • Both the operands are of the data type float. Then the result will also be of the same data type, as the float.
  • When one operand is of data type integer, and the other is of data type float. The online C compiler will return the result as a float value because the data type float is more precise in such cases to store the output than integers.

The Typecast operator

In C programming you can convert a value from one data type to another data type using the type cast operator. An integer when converted to float will have decimal values to six places, and afloat when converted to an integer will have its decimal values truncated. The typecast operator has precedence over multiplication, division, and modulo operators.

Syntax: (type_name) expression

Example:

Float to Integer conversion

Code:

Output:

Integer to Float Conversion

Code:

Output:

Example of Arithmetic Expression in C

Enough with the theory, Now, let's take an arithmetic expression in C and trace its execution.

Let's consider the Arithmetic Expression: -7*5/(3+2*3/12+8)+15*( (float)10 / 5)

The first precedence is given to parentheses, but here there are two parentheses in this expression, so let's start with the first one from the left.

  • Step 1:
    • We have to evaluate the expression within the first parenthesis, that is 3+2*3/12+8.
    • In this expression there are both multiplication and division. Both of these operators have the same precedence, so we will have to perform the multiplication first and then division because both the operators have associativity of Left to Right.
    • After executing 2*3 and the expression changes to 3+6/12+8
    • Now, we have to perform the division. The division of 6 by 12 will normally result in 0.5, but since both the operands here are integers, the result will also be converted to that of the integer data type. So when the float value gets converted to an integer, the decimal values get truncated and result in 0
    • Now the expression is of form 3+0+8
    • After the addition of 3 and 0, the expression becomes 3+8
    • And after performing this last addition, we get 11.
    • Now replace the result of this expression in the original expression.
  • Step 2:
    • Now we have the expression: -7*5/11+15*( (float)10 / 5). So we have to evaluate the second parenthesis and replace it with its result in this expression. Evaluate the expression: (float)10 / 5
    • The type cast operator has the maximum precedence, So after converting the 10 from integer to float, the expression becomes 10.0/5.
    • The division of 10 with 5 gives us a quotient of 2, a complete integer, but since one of the operands is float, the result should also be of the data type float. So the result is 2.0
    • Now replacing the result of this expression in the original expression, we get -7*5/11+15*2.0
  • Step 3:
    • Now there is a unary minus near 7, So we have to perform it next.
  • Step 4:
    • Then we perform the multiplication of -7 and 5 and replace their position in the original expression with the result, -35.
    • We have -35/11+15*2.0
  • Step 5:
    • Now perform the division of -35 and 11, since both the values are of integer data type, the result will be -3 and not -3.181.
    • Replacing -35/11 with -3, the expression becomes -3+15*2.0
  • Step 6:
    • Now we have to perform the multiplication of 15 and 2.0, the result will be 30.0, since one of the operands is a float value.
  • Step 7:
    • Now we have the expression -3+30.0, So the result is 27.0

So this is how the arithmetic expression gets evaluated, and any other answer other than 27.0 is wrong.

Conclusion:

  • Understanding arithmetic expressions in C involves knowing operators, operands, precedence, and associativity.
  • Types of expressions in C include arithmetic, relational, logical, and conditional.
  • Arithmetic expressions combine operands and arithmetic operators, following rules of precedence and associativity.
  • Evaluation of arithmetic expressions in C considers precedence, associativity, and data types of operands, impacting the final result.

Hope you learned something new from this article.

Thank You.