C examples for Operator:Arithmetic Operator
There are four basic arithmetic operators, which are + - * /, as well as the modulus operator %.
The modulus operator % is used to obtain the division remainder.
#include <stdio.h> int main(void) { float x = 3 + 2; /* 5 - addition */ x = 3 - 2; /* 1 - subtraction */ x = 3 * 2; /* 6 - multiplication */ x = 3 / 2; /* 1 - division */ x = 3 % 2; /* 1 - modulus (division remainder) */ }
The division sign operates on two integer values and will truncate the result and return an integer.
To get the correct value, one of the numbers must be explicitly converted to a floating-point number.
x = 3 / (float)2; /* 1.5 */