c中的typedef和#define是否相同? [英] Are typedef and #define the same in c?

查看:37
本文介绍了c中的typedef和#define是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 ?

I wonder if typedef and #define are the same in c?

推荐答案

没有.

#define 是一个预处理器标记:编译器本身永远不会看到它.
typedef 是编译器标记:预处理器不关心它.

#define is a preprocessor token: the compiler itself will never see it.
typedef is a compiler token: the preprocessor does not care about it.

您可以使用一种或另一种来达到相同的效果,但最好根据您的需要使用合适的一种

You can use one or the other to achieve the same effect, but it's better to use the proper one for your needs

#define MY_TYPE int
typedef int My_Type;

当事情变得棘手"时,使用适当的工具可以解决问题

When things get "hairy", using the proper tool makes it right

#define FX_TYPE void (*)(int)
typedef void (*stdfx)(int);

void fx_typ(stdfx fx); /* ok */
void fx_def(FX_TYPE fx); /* error */

这篇关于c中的typedef和#define是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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