MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Arithmetic Operators

The basic arithmetic operators in C are the same as in most other computer languages, and correspond to our usual mathematical/algebraic symbolism. The following arithmetic operators are present in C:

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modular Division

 

Some of the examples of algebraic expressions and their C notation are given below:

ExpressionC Notation
b* g d(b *g) / d
a^3 + cd

(????????)(a*a*a) + (c*d)


The arithmetic operators are all binary operators i.e. all the operators have two operands. The integer division yields the integer result. For example, the expression 10/3 evaluates to 3 and the expression 15/4 evaluates to 3. C provides the modulus operator, %, which yields the reminder after integer division. The modulus operator is an integer operator that can be used only with integer operands. The expression x%y yields the reminder after x is divided by y. Therefore, 10%3 yields 1 and 15%4 yields

3. An attempt to divide by zero is undefined on computer system and generally results in a run- time error. Normally, Arithmetic expressions in C are written in straight-line form. Thus ‘a divided by b’ is written as a/b.

The operands in arithmetic expressions can be of integer, float, double type. In order to effectively develop C programs, it will be necessary for you to understand the rules that are used for implicit conversation of floating point and integer values in C.

They are mentioned below:  

  1. An arithmetic operator between an integer and integer always yields an integer result.
  2. Operator between float and float yields a float result.
  3. Operator between integer and float yields a float result.

If the data type is double instead of float, then we get a result of double data type.

For example,

OperationResult
5/31
5.0/31.3
5/3.01.3
5.0/3.01.3

 

Parentheses can be used in C expression in the same manner as algebraic expression For example,

a * (b + c).

It may so happen that the type of the expression and the type of the variable on the left hand side of the assignment operator may not be same. In such a case the value for the expression is promoted or demoted depending on the type of the variable on left hand side of = (assignment operator). For example, consider the following assignment statements:

int i;
float b;
i = 4.6;
b = 20;

In the first assignment statement, float (4.6) is demoted to int. Hence i gets the value

4. In the second statement int (20) is promoted to float, b gets 20.0. If we have a complex expression like:  

float a, b, c;
int s;
s = a * b / 5.0 * c;

Where some operands are integers and some are float, then int will be promoted or demoted depending on left hand side operator. In this case, demotion will take place since s is an integer.

The rules of arithmetic precedence are as follows:

  1. Parentheses are at the “highest level of precedence”. In case of nested parenthesis, the innermost parentheses are evaluated first.
  2. Multiplication, Division and Modulus operators are evaluated next. If an expression contains several multiplication, division and modulus operators, evaluation proceeds from left to right. These three are at the same level of precedence.
  3. Addition, subtraction are evaluated last. If an expression contains several addition and subtraction operators, evaluation proceeds from left to right. Or the associativity is from left to right.

Apart from these binary arithmetic operators, C also contains two unary operators referred to as increment (++) and decrement (--) operators, which we are going to be discussed below:

The two-unary arithmetic operators provided by C are:

  1. Increment operator (++)
  2. Decrement operator (- -)

The increment operator increments the variable by one and decrement operator decrements the variable by one. These operators can be written in two forms i.e. before a variable or after a variable. If an increment / decrement operator is written before a variable, it is referred to as preincrement / predecrement operators and if it is written after a variable, it is referred to as post increment / postdecrement operator.

For example,

a++ or ++a is equivalent to a = a+1 and
a-- or - -a is equivalent to a = a -1

The importance of pre and post operator occurs while they are used in the expressions. Preincrementing (Predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Postincrementing (postdecrementing) the variable causes the current value of the variable is used in the expression in which it appears, then the variable value is incremented (decrement) by 1.

The explanation is given in the table below:

ExpressionExplanation
++aIncrement a by 1, then use the new value of a
a++Use the current value of a, then increment a by 1
--bDecrement b by 1, then use the new value of b
b--Use the current value of b, then decrement b by 1

 

Report an issue

Reporting: Arithmetic Operators (topic)

Related Posts