• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Variables » Arithmetic Expressions in C
Next →
← Prev

Arithmetic Expressions in C

By Dinesh Thakur

An arithmetic expression contains only arithmetic operators and operands. We know that the arithmetic operators in C language include unary operators (+ – ++ — ), multiplicative operators (* / %) and additive operators (+ – ). The arithmetic operands include integral operands (various int and char types) and floating-type operands (float, double and long double).

Example Simple arithmetic expressions

a) Several valid arithmetic expressions are given below. Assume that variables a and b are of type int.

                  valid arithmetic expressions

Observe that the operators are used correctly. The unary operators in expressions -1.23, a++ and -a + b /5 correctly operate on a single operand. The unary minus (-) is used as a prefix operator whereas the increment operator (++) is used in the postfix form. All other operators (multiplicative and additive) are used as infix operators and they operate on two operands. Also, the modulo-arithmetic(s) operator operates on integer operands as required. The expression ‘A’ + 2 is valid and its value is equal to 67, as the ASCII value of character ‘A’ is 65. Note that the operands and operators need not be separated by spaces. For example, the last two expressions in the first row can also be written as -a+b/5 and 3 .142*a*b. However, the spaces are often used to improve readability.

b) The expressions given below are invalidInvalid arithmetic expressions

The first two expressions are invalid because the operands a and b are not connected by any operator. This is true even for the third expression (a++ –b) as the++ and — operators operate on operands a and b, respectively, but there is no operator connecting these operands.

The last two expressions are invalid as they contain two binary operators in succession. These operators operate on a single operand instead of two. Note that the – operator in last expression cannot be interpreted as a unary operator as it does not precede an operand.

c) Can you tell which of the following expressions are valid?

which of the following expressions are valid

Surprisingly, all are valid C expressions although they do not appear to be so. In expression +a +b, the first+ operator is a unary operator that operates on variable a, whereas the second+ is a binary operator that connects variables a and b.

Now observe the space between the two’-‘ symbols in the second expression. They cannot

be interpreted as a decrement operator. As a result, the second – symbol is interpreted as a unary minus operator, whereas the first – is interpreted as a subtraction operator that operates on two operands a and -b. ·

The two consecutive operators in expression a % 10 I – b does not pose a problem as the –

operator is interpreted as a unary operator and the I operator operates on two operands (a % 10 and -b). However, the space between – and b is misleading.

In the last expression, observe the spaces separating the middle + symbol from the increment operators that operate on variables a and b. The middle + is interpreted as a binary operator that operates on a and b.

d) Several simple mathematical equations are given below along with equivalent C expressions.

Assume that the symbolic constant PI has been defined as

#define PI 3.1415927

simple mathematical equations equivalent C expressions

Observe that the literal 4 in expression 3 is written as floating constant 4.0 to avoid the integer division of 4 and 3. Similarly, constant 9 in expression 4 is also written as a floating constant. The sum of digits of a two-digit integer number is written as n I 10 + n % 10.Since n is assumed to be an integer variable, the expression n I 1o performs integer division and determines the digit at the 10’s place, whereas the expression n % 10 determines the digit at the unit’s place.

Evaluation of Simple Arithmetic Expressions

We use the operator precedence and associativity rules to determine the meaning and value of an expression in an unambiguous manner. Recall that the operators in an expression are bound to their operands in the order of their precedence. If the expression contains more than one operator at the same precedence level, they are associated with their operands using the associativity rules. Table summarizes these rules for arithmetic and assignment operators.

If the given expression is simple, we can often directly convert it to its mathematical form and evaluate it. However, if the given expression is complex, i. e., it contains several operators at different precedence levels, we need a systematic approach to convert it to a mathematical equation and evaluate it. The steps to convert a given valid C expression to its mathematical form (if possible) and to evaluate it are as follows:

1. First determine the order in which the operators are bound to their operands by applying the precedence and associativity rules. Note that after an operator is bound to its operand(s), that sub-expression is considered as a single operand for the adjacent operators.

2. Obtain the equivalent mathematical equation for given C expression (if possible) by following the operator binding sequence (obtained in step 1).

3. Determine the value of the given expression by evaluating operators in the binding sequence.

The steps to determine operator binding in an arithmetic expression are explained below with the help of the expression -a+ b * c – d I e + f.

1. The unary operators (unary +, unary – , ++ and – -) have the highest precedence and right-to-left associativity. Thus, the given expression is first scanned from right to left and unary operators, if any, are bound to their operands. The order is indicated below the expression as follows:

2. The multiplicative operators (*, I and %) have the next highest precedence and left to- right associativity. Thus, the expression is scanned from left-to-right and the multiplicative operators, if any, are bound to their operands as shown below. (Observe that after completion of the above step, sub-expressions -a, b * c and d I e will be operands for the remaining operator bindings.)

3. The additive operators (+ and -) have the next highest precedence and left-to-right associativity. Hence, the expression is scanned from left-to-tight and the additive operators, if any, are bound to their operands as shown below. Observe that the operands for the first + operator are the sub-expressions -a and b * c. Similarly, the operands for the – operator are -a+ b * c and d /e.

Now we can write the mathematical equation for the given C expression by following the operator binding sequence as shown below:

Now the given expression can be evaluated, again by following the operator binding sequence, as shown below. Assume that the variables a, b, c, ct, e and f are of type float and are initialized with values as a= 1. 5, b = 2. 0, c = 3. 5, ct= 5.0, e = 2. 5 and f = 1. 25.

Remember that except for some operators (& & || ? : and the comma operator), the C language does not specify the order of evaluation of sub-expressions. Thus, the sub expressions at the same level can be evaluated in any order. For example, the sub expressions -a, b * c and ct I e in the above expression can be evaluated in any order.

The procedure explained above can also be used to check the validity of an expression. The given expression is valid if we arrive at a single operand (or value) after all the operators in the given expression are considered. Consider a more complex arithmetic expression: -a–+ -b++ * –c. This expression appears to be invalid due to the excessive use of operators. It contains three operands a, b and c and seven operators, five of which are unary (-, ++ and –) and the other two are binary operators (+ and *). However, using the operator binding steps, we can easily verify that it is a valid expression:

You’ll also like:

  1. Write C++ program illustrates the hierarchy rule in an arithmetic expression involving arithmetic operators.
  2. What is Expressions ? Type of Expression
  3. What is Expressions in C++?
  4. Arithmetic Operators in C
  5. What is Arithmetic operator in C language?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW