Type cast

A type cast is a syntax that converts one type to another.

(Type name after conversion) Value

To convert an int8_t type value to an int32_t type value:

#include <stdint.h>
#include <stdio.h>

int main (void) {
  int8_t num_i8 = 5;
  int32_t num_i32 = (int32_t) num_i8;
  
  printf("%d\n", num_i32);
}

Be aware that type conversions can result in inaccuracies and incompatible types that can lead to unexpected values.

Some compilers warn you of things that are likely to be wrong, such as incompatible types.

For example, in gcc, " int32_t type If you typecast ">" to general-purpose pointer type "void *", the compilation will pass, but a warning will be issued.

#include <stdint.h>

int main (void) {
  int32_t num = 5;
  void * ptr = (void *) num;
}

Warning content.

a.c: 5: warning: cast to pointer from integer of different size

Implicit type conversion and type conversion rules

What happens to the result of type conversion is explained in Implicit type conversion and type conversion rules.

Associated Information