C++

What is Arithmetic Operators of C++

The arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). The first four operators are equivalent to the four fundamental rules of arithmetic. The operators can take operands of integral and floating types. The last operator—modulo—gives the remainder of integer division. If we mix integral and floating types in the expression, the result will have floating type. The modulo operator, however, can only have integral operands. The last assignment in the following code may give rise to a compiler warning as the result of the division is a double and is converted into an int.

For example:
int a = 10, b = 3, c;
c = a + b; // 13
c = a - b; // 7
c = a * b; // 30
c = a / b; // 3, integer division
c = a % 3; // 1, remainder
double d = 3.0;
c = a / d; // 3.333, floating type

6月 15, 2011

You Might Also Like

0 comments