c - c中typedef和#define一样吗?

标签 c macros c-preprocessor typedef

我想知道 中的 typedef#define 是否相同?

最佳答案

typedef 就像变量一样遵守作用域规则,而 define 一直有效,直到编译单元结束(或直到匹配的 undef) .

此外,有些事情可以使用 typedef 完成,但无法使用 define 完成。
例如:

typedef int* int_p1;
int_p1 a, b, c;  // a, b, c are all int pointers

#define int_p2 int*
int_p2 a, b, c;  // only the first is a pointer, because int_p2
                 // is replaced with int*, producing: int* a, b, c
                 // which should be read as: int *a, b, c
typedef int a10[10];
a10 a, b, c;  // create three 10-int arrays
typedef int (*func_p) (int);
func_p fp;  // func_p is a pointer to a function that
            // takes an int and returns an int

关于c - c中typedef和#define一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895489/

相关文章:

c - 如何用宏调用只有后缀不同的函数?

c++ - 宏中的波浪号 (~) 是什么意思?

c - 使用 C 返回数组

c - C语言数据链路层编程

c - 适用于 ARM Mac M1/M2 的 __rdtsc/__rdtscp?

c - 列出 C 常量/宏

c++ - 在 C 和 C++ 中解析 typedef

c - c 中的文件处理 : can not read proper in table format data

emacs - 如何在我的 Emacs 初始化文件中生成键盘宏?

c - 使用##连接宏常量