请问defined的这种谁用过??

RabbitLBJ 2012-12-18 03:21:01
#define CHINA
#define TAIWAN
#define JAPAN
// 以上为测试宏,可任意定义(注释)其中几个
#define IN_ASIA (defined(CHINA) || defined(TAIWAN) || defined(JAPAN))

#if IN_ASIA
...
#end

这种写法在VS中不可行,IN_ASIA一直都是FALSE
但是在arm的编译器里面貌似逻辑就是正确的,不知道是不是他的扩展?还是个什么情况??

另外标准的写法应该是
#define CHINA
#define TAIWAN
#define JAPAN

#if defined(CHINA) || defined(TAIWAN) || defined(JAPAN)
#define IN_ASIA TRUE
#else
#define IN_ASIA FALSE
#endif

下面的写法各个编译器均可以正确


...全文
431 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
马十一 2012-12-19
  • 打赏
  • 举报
回复
我记得,定义的形式为:#define 大写字符 常量。
  • 打赏
  • 举报
回复
MSDN中 The #if, #elif, #else, and #endif Directives相关的部分 This constant expression is considered true (nonzero) if the identifier is currently defined; otherwise, the condition is false (0). An identifier defined as empty text is considered defined. The defined directive can be used in an #if and an #elif directive, but nowhere else. 这样看来在#define 中的 defined直接认为是无效的了。
  • 打赏
  • 举报
回复
引用 10 楼 AnYidan 的回复:
为什么我记错了,因为有些 comipler 会将 #define CHINA CHINA 直接替换为 0,vs2010 显然不能,所以会提示 syntax error
你记错了,#define CHINA 不可能处理成#define CHINA 0的
RabbitLBJ 2012-12-18
  • 打赏
  • 举报
回复
引用 10 楼 AnYidan 的回复:
为什么我记错了,因为有些 comipler 会将 #define CHINA CHINA 直接替换为 0,vs2010 显然不能,所以会提示 syntax error
这个在vs2008里面也是不扩展的,感觉大都不扩展把,不然 #if #ifdef 就没什么区别了
whizer 2012-12-18
  • 打赏
  • 举报
回复
根据C标准,这种行为应该是不确定的。我把它摘了出来,大家看看吧。 Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator), just as in normal text. If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.
常如意 2012-12-18
  • 打赏
  • 举报
回复
没事多看书吧 搞这些没有用的
AnYidan 2012-12-18
  • 打赏
  • 举报
回复
为什么我记错了,因为有些 comipler 会将 #define CHINA CHINA 直接替换为 0,vs2010 显然不能,所以会提示 syntax error
RabbitLBJ 2012-12-18
  • 打赏
  • 举报
回复
引用 8 楼 AnYidan 的回复:
引用 6 楼 AnYidan 的回复: #define CHINA #define TAIWAN #define JAPAN #define IN_ASIA (defined(CHINA) || defined(TAIWAN) || defined(JAPAN)) 每一个都为 0, 0||0||0 自然也是0 sorry, 记错, vs2010 必须 定义为 ……
是啊,在VS里面基本上就是这么个意思 但是在arm c的编译器上面,defined(CHINA)会进行编译时的检查,而不仅仅是一段文字
AnYidan 2012-12-18
  • 打赏
  • 举报
回复
引用 6 楼 AnYidan 的回复:
#define CHINA #define TAIWAN #define JAPAN #define IN_ASIA (defined(CHINA) || defined(TAIWAN) || defined(JAPAN)) 每一个都为 0, 0||0||0 自然也是0
sorry, 记错, vs2010 必须 定义为 #define CHINA 0
图灵狗 2012-12-18
  • 打赏
  • 举报
回复
先Mark一下。
AnYidan 2012-12-18
  • 打赏
  • 举报
回复
#define CHINA #define TAIWAN #define JAPAN #define IN_ASIA (defined(CHINA) || defined(TAIWAN) || defined(JAPAN)) 每一个都为 0, 0||0||0 自然也是0
prajna 2012-12-18
  • 打赏
  • 举报
回复
测了一下 第一种 gcc 是可以的
赵4老师 2012-12-18
  • 打赏
  • 举报
回复
少纠结这种“语法糖”,好钢用在刀刃上。
prajna 2012-12-18
  • 打赏
  • 举报
回复
第一种应该是不合语法吧 第二种很好
RabbitLBJ 2012-12-18
  • 打赏
  • 举报
回复
自己顶一下,大牛说说第一种用法到底是编译器特殊支持呢?还是C语言本来就可以这样用??个人感觉VS2008都不支持,应该不是C语言的标准用法把???
东大坡居士 2012-12-18
  • 打赏
  • 举报
回复
in my opinion, 我学得OK

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧