Type conversion rules for integer and floating point operations in C language
In C, different data types will be performed during operation.Implicit type conversion。
whenSigned integers (int
)、Unsigned integers (unsigned int
)andFloating point type (float
、double
)When performing calculations,The compiler will automatically adjust the data type of the operation according to the type priority and conversion rules.。
1. Int and unsigned int conversion rules when participating in operation
whenint
andunsigned int
Perform operations, the calculation result is usually converted tounsigned int
, the reasons are as follows:
(1) Unsigned Dominance Rule
C language regulations:
If an operand isint
, another operand isunsigned int
, and they have the same width (like 32 bits), thenint
Will be promoted tounsigned int
。
(2) Example
#include <> int main() { int a = -5; unsigned int b = 10; if (a < b) { // a will be converted to unsigned int printf("a < b is true\n"); } else { printf("a < b is false\n"); } return 0; }
Output:
a < b is false
analyze:
-
a
yes-5
(int
),b
yes10
(unsigned int
)。 -
a
It will be converted tounsigned int
,-5
Become4294967291
(Under 32-bit system). -
4294967291 > 10
,soa < b
becomefalse
, contrary to intuition.
(3) Methods to avoid problems
- Use explicit type conversion
if ((int)a < (int)b) { printf("Correct comparison\n"); }
-
avoid
int
andunsigned int
Mixed
unsigned int a = 5; unsigned int b = 10;
-
use
size_t
Make a safe comparison-
size_t
It is an unsigned integer, suitable for array indexing and other situations.
-
2. Unsigned int and floating point numbers (float / double) conversion rules when participating in operations
whenunsigned int
andfloat
/ double
Perform operations, the calculation result is usually converted toFloating point type (float
/ double
), the reasons are as follows:
(1) Floating point type is preferred
C language regulations:
If an operand isunsigned int
, another operand isfloat
ordouble
,butunsigned int
It will automatically convert tofloat
/ double
。
(2) Why not convert to unsigned int?
-
The representation range ratio of floating point numbers
unsigned int
Bigger- 32 bits
unsigned int
The maximum value is4294967295
(2^32 - 1
)。 -
float
Can be expressed~3.4 × 10^38
,double
Can be expressed~1.8 × 10^308
。 -
The range of floating-point numbers can be represented far exceeds that of unsigned integers, so the conversion direction is
unsigned int → float/double
, without reverse conversion.
- 32 bits
-
Floating points can represent decimals, but integers cannot
- For example:
5 / 2.0 = 2.5
, if converted tounsigned int
, will be lost.5
,become2
, which results in a loss of accuracy. -
To avoid accuracy loss, C language will default to
unsigned int
Convert tofloat
ordouble
Perform calculations.
- For example:
(3) Example
#include <> int main() { unsigned int a = 10; float b = 3.5; float result = a + b; // `a` convert to `float` printf("Result: %f\n", result); return 0; }
Output:
Result: 13.500000
analyze:
-
a
(unsigned int
) is converted tofloat
, becomes10.0f
。 - calculate
10.0f + 3.5f = 13.5f
, the result type isfloat
。
3. Summary of type conversion rules
Operation type | Result Data Type | illustrate |
---|---|---|
int + unsigned int | unsigned int | int is converted to unsigned int first, which may cause negative numbers to become larger |
unsigned int + float | float | unsigned int is converted to float first, and then calculated |
unsigned int + double | double | unsigned int is converted to double first, and then calculated |
unsigned int * float | float | unsigned int is converted to float first, and then calculated |
unsigned int * double | double | unsigned int is converted to double first, and then calculated |
Key summary
-
int
andunsigned int
When computing,int
Will be converted tounsigned int
:- A negative number may become a very large positive number, resulting in a logical error.
-
unsigned int
andfloat/double
When computing,unsigned int
Will be converted tofloat/double
:- Avoid accuracy loss and ensure the accuracy of floating-point calculations.
-
To avoid type conversion problems, it is recommended:
- Unify variable types to avoid
int
andunsigned int
Mixed use. - Use explicitly
float
ordouble
Do floating point calculations and do not rely on implicit conversions. - use
size_t
Process array indexes to avoidunsigned int
Problems brought about.
- Unify variable types to avoid
These rules apply to C, and also to C++ and other similar programming languages.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.