Operations. Arithmetic operations
Contents
- 1. What arithmetic operations are used in C/C++?
- 2. For what types of data you can use the arithmetic operations?
- 3. How is realized a cast in operations +, –, *?
- 4. What is the priority and associativity of arithmetic operations?
- 5. What is the difference between binary and unary addition (+) and subtraction (–) operations?
- 6. What are the features of using the operation % (remainder of division)?
- 7. What are the features of using the operation / (division)?
- Related topics
Search other websites:
1. What arithmetic operations are used in C/C ++?
In C/C++, the following arithmetic operations are supported:
+ – addition; - – subtraction; * – multiplication; / – division % – remainder of division.
All these operations are binary. This means that to get the result, you need 2 operands. General view of the arithmetic operation:
operand1 operation operand2
where
- operation is one of the operations +, –, *, %, /.
2. For what types of data you can use the arithmetic operations?
Arithmetic operations can be used for:
- integer types: short int, unsigned short int, int, unsigned int, long, unsigned long;
- floating point types: float, double, long double;
- types (classes) that contain “overloaded” arithmetic operations.
In arithmetic operations, one of the operands can have an integer type, and the second can be a floating point type. In this case, the result is of a floating-point type.
3. How is realized a cast in operations +, -, *?
In the expressions, where used operations +, *, –, the following rules of type cast are used:
- if both operands are of an integer type, the result will also be of an integer type;
- if at least one of the operands has a real type and another is an integer type, then the result will also be of a real type;
- if one of the operands is of type float, and another is of type double, then the result is of type double. This is due to the fact that the ‘double’ type requires more memory than the ‘float’ type. In this case, the ‘float’ type is extended to ‘double’ type.
4. What is the priority and associativity of arithmetic operations?
Arithmetic operations have priority and associativity, as shown in the following table.
Signs of operations |
Name |
Associativity |
* / % |
Binary, multiplicative | From left to right |
+ – |
Binary, additive | From left to right |
5. What is the difference between binary and unary addition (+) and subtraction (-) operations?
The operations of addition (+) and subtraction (-) can be either binary or unary.
Binary operations + and – are used in expressions when performing calculations.
Unary ‘+’ and ‘-‘ operations are used to denote a number (a positive number or a negative number).
Example.
int a, b; a = -8; // unary operation '-', indicates the sign of a number b = +9; // unary operation '+', b = 9 a = b-5; // binary operation '-', is used to calculate the expression
6. What are the features of using the operation % (remainder of division)?
The operation ‘%’ is used on integer operands. The operation ‘%’ allows you to get the remainder of the division of the integer operands.
Example.
// operation % - reminder of the division int a, b; int c; a = 3; b = 5; c = a % b; // c = 3 a = 8; b = 4; c = a % b; // c = 0 c = 12 % 35; // c = 12 c = 35 % 12; // c = 11 c = -5 % -3; // c = -2
7. What are the features of using the operation / (division)?
The operation of division has its own peculiarities, which consist in the following:
- if two operands have an integer type, the result is returned as an integer type. In this case, an integer division occurs. The remainder of the division is truncated;
- if one of the operands is of a floating-point type, then the result also has a floating-point type.
Example.
// the operation of division int a, b; int c; float x; a = 8; b = 3; c = a / b; // c = 2 x = a / b; // x = 2.0 x = a / (float)b; // x = 2.666667 x = 17.0 / 3; // x = 5.666667 x = 17 / 3; // x = 5.0
Related topics
- Operations of relations
- Logical operations. Bitwise logical operations. Shift operations
- Operators of increment (++) and decrement (—). The complex assignment operators
- The operation priority table in C++