- C language
- Basic grammar
- here

## Implicit type conversion

Explains implicit type conversion in C language.

### Assignment to a different type

In C language, if you assign to a different type, it will be converted to the value of that type.

#include <stdint.h> #include <stdio.h> int main (void) { int32_t num_i32 = 1000; int8_t num_i8 = num_i32; printf("%d\n", num_i8); }

This is the output result.

-twenty four

It should be 1000, but it is -24. This is due to the implicit type conversion from int32_t to int8_t. Please wait until the type conversion rule is explained to explain why it becomes "-24".

### Pass to arguments of different types of functions

When you pass it to a function argument, it does the same implicit type conversion as an assignment to a different type. Since the argument type is int8_t and the type of the value passed by the caller is int32_t type, this is converted to int8_t type.

#include <stdint.h> #include <stdio.h> void print_i8 (int8_t num_i8) { printf("%d\n", num_i8); } int main (void) { int32_t num_i32 = 1000; print_i8 (num_i32); }

### Integral promotion --Operations with values smaller than int type are converted to int type

int type Operations with values smaller than int type are converted to int type. This is called integral promotion. This is for integer unary operations and floating point unary operations. , When using the unary "-", it occurs when using comparison operator.

For example, let's say you have calculated the value of int8_t. Instead of being calculated with the size of int8_t, it is type-converted to an int type, a numerical value is calculated, and then it is type-converted to int8_t again.

In the sample below, I wrote it explicitly using type cast.

#include <stdio.h> #include <stdint.h> int main (void) { int8_t num1 = 5; int8_t num2 = 10; / * int8_t num3 = num1 + num2; * / / // Write the above process explicitly using typecast int8_t num3 = (int8_t) ((int) num1 + (int) num2); / * int8_t num4 = -num3; * / / // If written explicitly, the above process will be as follows int8_t num4 = (int8_t) (-(int) num3); }

### In the operation, the type with the smaller order is converted to the larger type

Four arithmetic operations on integers and Four arithmetic operations on floating point numbers are unary When using "-", when using comparison operator, the type with the smaller type order is converted to the larger type. ..

It's hard to remember all the type ordering rules in C, so at least remember the following order:

Signed and unsigned integers are not mixed, and it is assumed that routine calculations are done with signed integers and floating point numbers.

Floating point and Signed integer type order (larger left) | double, float, int64_t, int32_t, int16_t, int8_t < / td> |

Unsigned integer type order (larger left) | uint64_t, uint32_t, uint16_t, uint8_t |

Floating-point types have the largest order of types than any integer type. An integer type with a large number of bits is larger than an integer type with a small number of bits.

In the sample below, I wrote it explicitly using type cast. A value of type int32_t is converted to a double type with a large type order.

#include <stdio.h> #include <stdint.h> int main (void) { int32_t num1 = 5; double num2 = 10.5; / * double num3 = num1 + num2; * / / // Write the above process explicitly using typecast double num3 = (double) num1 + num2; printf("%f", num3); }