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:
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modular Division |
Some of the examples of algebraic expressions and their C notation are given below:
| Expression | C Notation |
|---|---|
| b* g d | (b *g) / d |
| a^3 + cd | (a*a*a) + (c*d) |
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:
If the data type is double instead of float, then we get a result of 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 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:
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:
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:
| Expression | Explanation |
|---|---|
| ++a | Increment a by 1, then use the new value of a |
| a++ | Use the current value of a, then increment a by 1 |
| --b | Decrement b by 1, then use the new value of b |
| b-- | Use the current value of b, then decrement b by 1 |