## Jun 8, 2014

### C Programming #07: Operators - Arithmetic

Arithmetic operators that are supported in C :
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Remainder (%)

'+' is addition operator in C. (intuitive: same as maths). It can be used  with both variables and constant integers, float and characters (ASCII value would be used in case of characters). If addition operator is used with strings ,then compiler would just throw an error. Therefore C does not support string addition.

Program showing use of addition operator

#include <stdio.h>

int main()
{
int a = 10;
int b = 30;

float c = 1.3;
float d = 4.1;

char e = 'a';
char f = 'b';

printf("Sum of a(int) and b(int) is %d\n", a + b);
printf("Sum of a(int) and 10(int) is %d\n", a + 10);
printf("Sum of 10(int) and 20(int) is %d\n", 10 + 20);

printf("Sum of c(float) and d(float) is %f\n", c + d);
printf("Sum of c(float) and 2.1(float) is %f\n", c + 2.1);
printf("Sum of 2.1(float) and 3.4(float) is %f\n", 2.1 + 3.4);

printf("Sum of e(char) and f(char) is %d\n", e + f);
printf("Sum of e(char) and 'z'(char) is %d\n", e + 'z');
printf("Sum of 'c'(char) and 'e'(char) is %d\n", 'c' + 'e');

return 0;
}


Output of above program is

Sum of a(int) and b(int) is 40
Sum of a(int) and 10(int) is 20
Sum of 10(int) and 20(int) is 30
Sum of c(float) and d(float) is 5.400000
Sum of c(float) and 2.1(float) is 3.400000
Sum of 2.1(float) and 3.4(float) is 5.500000
Sum of e(char) and f(char) is 195
Sum of e(char) and 'z'(char) is 219
Sum of 'c'(char) and 'e'(char) is 200


Food for thought: What will happen when we mix type say for  example we add character to an integer ?
Answer: C supports adding number of different type (character, integer, long, float, double).
Say i do
char var = 'a';
var + 10;
Result of it will be ASCII of 'a' + 10 i.e 107. Internally character is represented in 1 byte and 10 as 4 bytes. Result of which is 107 which is an integer.  C makes sure that when the different data types  are used result will be of size of biggest sized operand. In this example the char 'a' occupies 1 byte where as integer 10 occupies 4 bytes , hence the sum 107 occupies 4 bytes since the integer size is bigger than character size.This is called as data type up-gradation. Same logic can be used for other data types also.

Order of upgrading data type is as follows
char < int < float < double

## Subtraction

'-' is subtraction operator in C. (intuitive: same as maths). Data type up gradation rules that apply to addition also applies to subtraction. I don't think repeating will add any extra information.

## Multiplication

'*' is multiplication operator in C. (counter intuitive: x is used in maths). Data type up gradation rules that apply to addition also applies to multiplication. I don't think repeating will add any extra information.

## Division

'/' is division operator in C. (almost intuitive: similar used in maths). Division has some caveats in C, which needs special attention.

1. dividend is integer, divisor is integer

For example:  7/2 is 3
7 and 2 are integers, hence C tries to keep the result also as integer. Hence the truncation.
-7/2 is -3
C makes sure that sign is correct.

2. dividend is integer, divisor is float

For example: 7/2.0 is 3.5
7 is integer while 2.0 is double, hence result will be double (remember the data up gradation) Hence the result is 3.5

In all cases sign of result is taken care of properly.

Above two example is making C little bit complicated. But I think after you go through the process in which C decides how to do the operation it becomes clear.

The idea is not to fit C into our thought process, but to fit our thought process into how C would work. Only practice would make it happen.

## Remainder

'%' is remainder operator in C. (it is very similar to mod operator in maths).

It can work only on integer type of data type (character, integer, long). It will not work on float or double. Since the remainder operator works like modular operator it will not work on float and double.
You can quickly go through excellent article from Khan Academy - link.

Examples:

5%3 is 2
-5%3 is -2 (which is same as 1 in modulo arithmetic).

Hence Remainder in C is quirky ;)