# C语言宏的几个注意点

104人阅读 评论(0)

• 宏定义
宏定义中要注意的主要有两点，一点是宏参数可能被evaluation多次。比如：
#define max(A, B) ((A) > (B) ? (A) : (B))
max(i++, j++) /* i和j中较大的值会被evaluation2次*/

#define square(x) x*x
square(z+1)
• 条件包含
这里面主要是关于宏的理解。
#define name replacement text

subsequent occurrences of the token name will be replaced by the replacement text. The name in a #define has the same form as a variable name; the replacement text is arbitrary. Normally the replacement text is the rest of the line, but a long definition may be continued onto several lines by placing a \ at the end of each line to be continued.

#if !defined(HDR)
#define HDR
/* contents of hdr.h go here */
#endif

#ifndef HDR
#define HDR
/* contents of hdr.h go here */
#endif

The first inclusion of hdr.h defines the name HDR; subsequent inclusions will find the name defined and skip down to the #endif. A similar style can be used to avoid including files multiple times. If this style is used consistently, then each header can itself include any other headers on which it depends, without the user of the header having to deal with the interdependence.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：25199次
• 积分：944
• 等级：
• 排名：千里之外
• 原创：60篇
• 转载：18篇
• 译文：9篇
• 评论：5条
阅读排行
评论排行
最新评论