Arithmetic Operators in C Language

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:

The following arithmetic operators are present in C:

Arithmetic Operators in C

Operator Meaning
+ Addition
Subtraction
* Multiplication
/ Division
% Modular Division

 

Some of the examples

(b *g) / d
(a*a) + (c*d)

All these arithmetic operators are 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 remainder 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 the computer system and generally results in a runtime error.

Arithmetic expressions

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 conversion of floating point and integer values in C.

They are mentioned below:
• An arithmetic operation between an integer and integer always yields an integer result.
• Operation between float and float yields a float result.
• Operation between integer and float yields a float result.

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

Operation Result
5/3 1
5.0/3 1.3
5/3.0 1.3
5.0/3.0 1.3

 

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

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 of 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:

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” get 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 multiplications, division and modulus operators, evaluation proceeds from left to right. These three are at the same level of precedence.
  3. Addition, subtraction is evaluated last. If an expression contains several additions and subtraction operators, evaluation proceeds from left to right. Or the associativity is from left to right.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
?>