类型定义和界定的区别 [英] difference between typedef and define

查看:141
本文介绍了类型定义和界定的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  是的typedef和#定义在C一样吗?

  通过的#define和困惑的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);

这两项测试编译和运行没有问题。因此,他们是等价?

Both tests compile and run without problems. So, are they equivalent?

感谢。

推荐答案

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

There is a difference when you want to create an alias of a pointer type.

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 遵守规则范围,的的你可以声明一个类型局部块。

Moreover, typedef obeys scope rules, i.e. you can declare a type local to a block.

在另一方面,你可以使用的#define 当你想要管理在preprocessor指令类型。

On the other hand, you can use #define when you want to manage your type in a preprocessor directive.

这篇关于类型定义和界定的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆