C programming tutorial
Following are the arithmetic operators in C.
+
-
*
/
%
++
--
+,-,*,/ are obvious.They represent addition, subtraction, multiplication and division respectively.
Following are some important points about division operator which you should keep in mind.
Suppose a is an integer variable and b is an float variable.This means a will always hold integer and b will always hold real number.Following will be result of division operator.
| instruction | result | instruction | result |
|---|---|---|---|
| a=2/9 | 0 | b=2/9 | 0.0 |
| a=2.0/9 | 0 | b=2.0/9 | 0.2222 |
| a=2/9.0 | 0 | b=2/9.0 | 0.2222 |
| a=2.0/9.0 | 0 | b=2.0/9.0 | 0.2222 |
| a=9/2 | 4 | b=9/2 | 4.0 |
| a=9.0/2 | 4 | b=9.0/2 | 4.5 |
| a=9/2.0 | 4 | b=9/2.0 | 4.5 |
| a=9.0/2.0 | 4 | b=9.0/2.0 | 4.5 |
In C % means remainder.It gives the remainder of two numbers.
example
result of 3%2 will be 1 because when 3 is divided by 2 it gives remainder 1
result of 10%2 will be 0 because when 10 is divede by 2 remainder comes to be zero.
Increment operator increase the value of variable by 1.
hence ++i is same as i=i+1.
Increment operator can be used in two forms,prefix and suffix.
In prefix,++ is written before variable.ex. ++a
In suffix,++ is written after variable.ex.a++
Both increment the value of variable by 1,Then you will ask whats the difference between prefix and suffix.I will explain it using following examples.
int a = 2 ,b=3,p;
p= b * a++;
In this statement firstly b will be multiplied with a then value of a will be incremented by 1.Hence value of
Value of p will be 6 and value of a will become 3 after execution of this statement.
int a=2 ,b=3,p;
p=b * ++a;
In this statement firstly a will be incremented by 1.Then it will be multiplied with b.Hence after execution of this statement value of a will become 3 and value of p will be 9.