The difference between typedef and #define usage

foreword

The blogger saw a topic about typedef and #define on Niuke.com. I found that many beginners are not particularly clear about the usage of the two, so the blogger summarizes the following related usage and differences here.
Without further ado, let’s take a look at the original question first! (answer c)
insert image description here

1. Different principles

#define is a grammar defined in C language, and it is a preprocessing instruction. Simple and mechanical replacement is performed during preprocessing, and no correctness check is performed. Only when the source program that has been expanded will it find possible errors and report an error.

For example: in the
#define PI 3.1415926 program: area=PI *r *r will be replaced with 3.1415926 *r *r If you write the number 9 in the #define statement as the letter g, the preprocessing will also be brought in.

typedef is a keyword, which is processed at compile time and has a type checking function. It aliases an existing type in its own scope, but cannot use a typedef inside a function definition. Using typedef to define types such as arrays, pointers, and structures will bring great convenience. It not only makes program writing simple, but also makes the meaning clear and enhances readability.

1.1 typedef int * int_ptr; and #define int_ptr int * explained in detail

In the problem we saw at the beginning, typedef int * int_ptr; and #define int_ptr int *
both use int_ptr to represent int *, but the two are different. As mentioned earlier, #define performs simple replacement during preprocessing, while typedef is not a simple replacement, but declares a type in the same way as defining a variable. That is to say;

#define int_ptr int *
int_ptr a, b; // equivalent to int * a, b; just a simple macro replacement

typedef int* int_ptr;
int_ptr a, b; //a, b are pointers to int, typedef introduces a new mnemonic for int*

This also explains why the following views hold

 typedef int * pint ;
#define PINT int *

那么:
const pint p ;//p不可更改,但p指向的内容可更改
const PINT p ;//p可更改,但是p指向的内容不可更改。

pint是一种指针类型 const pint p 就是把指针给锁住了 p不可更改const PINT p 是const int * p 锁的是指针p所指的对象。

2. Different functions

  • typedef is used to define the alias of the type, which has the function of making the type easy to remember. Another feature is the definition of machine-independent types. For example, if you define a REAL floating-point type, it can obtain the highest precision on the target machine: typedef long double REAL, on a machine that does not support long double, it looks like this, typedef double REAL; on a machine that does not support double Above, it is like this, typedef float REA
  • #define can not only alias the type, but also define constants, variables, compilation switches, etc.

3. Different scope

  • #define has no scope limitation, as long as it is a previously predefined macro, it can be used in future programs, while typedef has its own scope.

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/131098433