- C language
- Basic grammar
- here
Four arithmetic operations of integers
Let's perform four arithmetic operations on integers . Adds, subtracts, multiplies, and divides integers.
Four arithmetic operations on int32_t type values
Let's perform four arithmetic operations with the int32_t type. The point of division is that the result does not have a decimal point. The decimal point is truncated.
The maximum value of an integer that can be represented by a signed 32-bit integer type is "2147483647", and the minimum value is "-2147483648".
The maximum value is " INT32_MAX" and the minimum value is " INT32_MIN". It is defined.
To output, specify "%d" in the format specifier of printf function.
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num1 = 5; int32_t num2 = 2; int32_t add = num1 + num2; int32_t sub = num1 --num2; int32_t mul = num1 * num2; int32_t div = num1 / num2; printf("add:%d\nsub:%d\nmul:%d\ndiv:%d\n", add, sub, mul, div); }
This is the output result.
add: 7 sub: 3 mul: 10 div: 2
Four arithmetic operations on int64_t type values
Let's perform four arithmetic operations with the int64_t type.
The maximum value of an integer that can be represented by a signed 64-bit integer type is "9223372036854775807", and the minimum value is "-9223372036854775808".
The maximum value is " INT64_MAX" and the minimum value is " INT64_MIN". It is defined.
To output, specify "PRId64" as the format specifier of printf function. This is a bit annoying, but to avoid portability issues.
#include <stdio.h> #include <stdint.h> #include <inttypes.h> int main (void) { int64_t num1 = 5; int64_t num2 = 2; int64_t add = num1 + num2; int64_t sub = num1 --num2; int64_t mul = num1 * num2; int64_t div = num1 / num2; printf("add:%" PRId64 "\ nsub:%" PRId64 "\ nmul:%" PRId64 "\ ndiv:%" PRId64 "\ n", add, sub, mul, div); }
This is the output result.
add: 7 sub: 3 mul: 10 div: 2
Notes on the four arithmetic operations of integers in C language
The rules of four arithmetic operations for integers in C seem simple and are surprisingly complicated. First of all, I will write the complicated reason.
Distinguishing between signed and unsigned integer types
First, in C language, there are two types, signed integer type and unsigned integer type.
Now, what happens to the result if we operate on signed and unsigned integer types? What is the result type and what is the result value? That's the question.
The specification of C type automatic conversion rules is complicated and difficult to remember.
So, here, let's write the best practices first.
Do not perform operations that mix signed integer types and unsigned integer types
Do not perform operations that mix signed integer types and unsigned integer types. If you need to perform an operation, use type conversion to make it a signed integer type or an unsigned integer type.
Integer operations are performed in signed integer types
Integer operations are performed on signed integer types.
Integer operations are performed between signed integer types as much as possible. Unsigned 64-bit integer type uint64_t type operations are required very occasionally, but other operations are signed integers of 64 bits or less. Type ( int64_t, int32_t, int16_t, int8_t).
The first choice is a signed 32-bit integer type
The first integer type to select in integer arithmetic is signed 32-bit integer type int32_t.
#include <stdint.h> int main (void) { // Signed 32-bit integer type int32_t num = 3; }
Signed 32-bit integers are the most basic type of integer type.
Signed 32-bit integer operations use less memory and are faster than signed 64-bit integer operations. I don't know how fast it is, but considering the architecture, I don't think it will be slower than 64-bit integer arithmetic.
If you need a signed 64-bit integer operation, use the signed 64-bit integer type int64_t.
Signed 16-bit integer types and signed 8-bit integer types are not selected because in many processing systems, these types are expected to be expanded to signed 32-bit integers and calculated on the CPU. This is called an integral promotion in the C language specification.
According to the C language specification, signed integer types (short and signed char) smaller than int type are extended to int. The definition in the C language int specification is "integer type with a minimum width of 16 bits". However, in 2020, in many implementations, you can assume that the int type is 32bit. In other words, assume that signed integers smaller than 32 bits are type-extended to signed 32 bit integers.
Automatic type expansion conversion in signed integer type operations
In signed integer type operations, the smaller width type is expanded to the wider type and calculated.
#include <stdint.h> int main (void) { // Signed 32-bit integer type and signed 64-bit integer type operations // num1 is type converted to int64_t int32_t num1 = 3; int64_t num2 = 5; int64_t num3 = num1 + num2; }
It has the same meaning as below.
int64_t num3 = (int64_t) num1 + num2;
You can rest assured that the value of the contents does not change when expanding the type in a signed integer type.
If you want to know more about type conversion, see C type conversion rules.
What happens if the integer exceeds the maximum value?
Let's add 1 to the maximum value of the integer. What will happen?
#include <stdio.h> #include <stdint.h> int main (void) { int32_t num = 2147483647; num ++; printf("%d\n", num); }
This is the output result. It is the minimum value.
-2147483648
If a negative number is in 2's complement representation, add 1 to the maximum value to get the minimum value.