How to understand the use of C language define and typedef?

/ definition of common data types em >

-sharpdefine U8 uint8_t
-sharpdefine U16 uint16_t
-sharpdefine U32 uint32_t
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;

based on the context, I guess the following two sentences mean something like this:
typedef unsigned char unit8_t;
-sharpdefine U8 unit8_t;

The previous sentence defines a new type of unit8_t, which is actually an alias for unsigned char.
the next sentence is because unit8_ t is too long to write, so a macro u8 is defined, which is actually the calling unit8_t.

so here comes the problem:
1. Follow the program initialization procedure
-sharpdefine U8 unit8_t;
typedef unsigned char unit8_t;

the code in the figure is first-sharpdefine and then typedef. Is it reversed?

2. Can you simplify the two sentences into one?

typedef unsigned char U8


  1. Macro definition define takes effect on the entire file, of course, it is defined in the header, it will replace the predefined content in the whole file accordingly. To put it simply, the mutation does not check whether U8 and uint8_t are defined, because they are not variables in nature. So, it doesn't contradict typedef unsigned char unit8_t .
  2. can you combine two sentences into one? the answer is yes, but this definition is not intuitive, overly concise, and sometimes not very good.

in fact, there is no direct relationship between the two statements.

-sharpdefine is a macro definition and belongs to the preprocessing phase. That is, in the preprocessing phase, the preprocessor replaces all U8 in the program with uint8_t .
typedef is a type definition, which is equivalent to defining a new type uint8_t , which you must define if you use uint8_t in your code.

  1. generally speaking, you only need to have typedef unsigned char unit8_t before using U8 and uint8_t .
  2. of course you can write that
Menu