【问题标题】:difference between typedef and define [duplicate]typedef和define之间的区别[重复]
【发布时间】:2012-08-25 11:55:30
【问题描述】:

可能重复:
Is typedef and #define the same in c?
Confused by #define and typedef

以下有什么区别:

#define NUM int

...

NUM x;
x = 5;
printf("X: %d\n", x);

还有这个:

typedef int NUM;

...

NUM x;
x = 5;
printf("X : %d\n", x);

两个测试都可以顺利编译和运行。那么,它们是等价的吗?

谢谢。

【问题讨论】:

    标签: c


    【解决方案1】:

    当你想创建一个指针类型的别名时是有区别的。

    typedef int *t1;
    #define t2 int *
    
    t1 a, b; /* a is 'int*' and b is 'int*' */
    t2 c, d; /* c is 'int*' and d is 'int'  */
    

    此外,typedef 遵守范围规则,您可以声明块的本地类型。

    另一方面,当你想在预处理器指令中管理你的类型时,你可以使用#define

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2011-07-09
      • 2012-10-11
      • 2017-05-13
      • 2014-08-16
      • 2011-04-08
      • 2012-11-24
      • 2013-06-05
      相关资源
      最近更新 更多