LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3736|回复: 11

哪位提供一些#define用法的案例

[复制链接]
发表于 2006-9-28 15:29:19 | 显示全部楼层 |阅读模式
觉得 #define  挺好用的
想学习一下,可是网上搜索没有找到


哪位帮助一下 提供些宏定义的具体用法 谢谢了
发表于 2006-9-28 22:36:47 | 显示全部楼层
#define 就是宏替换, 其实没什么好说的. 一般来说无外乎

  1. /* 最基本的定义常量 */
  2. #define N 10
  3. int nums[N];
复制代码


  1. /* use with #if or #ifdef */
  2. #define DEBUG 1
  3. /* xxxxx */
  4. #if DEBUG
  5.   printf("debug info...\n");
  6. #endif
复制代码


  1. /* 与上面类似, 但值得单独提一下.
  2. * 避免头文件重复引用. 比如这是 test.h */
  3. #ifndef __TEST_H__
  4. #define __TEST_H__
  5. /* xxxx */
  6. #endif
复制代码


  1. /* 定义 "内联" 函数. 现在 C 也支持 inline 了, 所以这种用法不必要了 */
  2. /* 较短的一般这样写 */
  3. #define HEX2CHR(x) (x < 10) ? (x + '0') : (x - 10 + 'A')
  4. /* 较长的一般这样写 */
  5. #define LOG(msg) do { \
  6.   printf(msg); \
  7.   fprintf(flog, msg); \
  8. while (0)
  9. /* xxxx */
  10. char c = HEX2CHR(12);
  11. LOG("haha\n");
复制代码


我自己用过的也就是这些. 但不管怎么用, #define 的功能还只是简单的宏替换而已
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-9-29 14:13:01 | 显示全部楼层
呵呵  我只是觉得宏定义的 函数之类的挺牛的样子
其实很复杂的用法我从来没用过

谢谢你的回答

我只是想把这个东西做为一个乐趣研究一下
看看有哪些让人晕乎的用法
回复 支持 反对

使用道具 举报

发表于 2006-9-29 15:03:51 | 显示全部楼层
Post by lpsir
呵呵  我只是觉得宏定义的 函数之类的挺牛的样子
其实很复杂的用法我从来没用过

谢谢你的回答

我只是想把这个东西做为一个乐趣研究一下
看看有哪些让人晕乎的用法


其实可以把它看作一个很小的语言
但是是在编译之前执行
因此,可以用来处理一些在编译时就确定的量

例如:


  1. #define NELEMS(x) ((sizeof(x)) / (sizeof((x)[0])))

  2. main()
  3. {
  4.     int a[100];

  5.     assert(NELEMS(a) == 100);
  6. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-9-29 16:25:35 | 显示全部楼层
我是想系统的知道  #define怎么用
比如说
函数
Writelog(char *p,int l,char *fmt,...);
#define WriteLogM(fmt...) Writelog(__FILE__,__LINE__,fmt)

这样以后就可以用WriteLogM(fmt...) 了
但是我不明白 ,为什么可以这样设置
比如为什么不是
#define WriteLogM(fmt,...) Writelog(__FILE__,__LINE__,fmt)
或者是
#define WriteLogM(fmt...) Writelog(__FILE__,__LINE__,fmt,...)

第一次用的话没有这方面的知识很难定义对啊
所以我想学习一下这方面的知识,可是苦于没有系统的学习资料
回复 支持 反对

使用道具 举报

发表于 2006-9-29 23:02:20 | 显示全部楼层
楼主要明白, #define 的功能是替换. 这分几种情况
没有参数的情况
有固定参数的情况
参数个数不定的情况

前两种都好理解, 也好说明, 所以就不说了. 我按照自己的经验说明一下最后一种:

假如有
#define fn(a, b, c...) func(a, b, c)
然后你这样调用
fn(1, 2, 3, 4)
那么, 它将被替换成如下的形式
func(1, 2, 3, 4)

为什么呢?
在替换过程中, fn 中的 a 对应 "1", b 对应 "2", c 对应 "3, 4", 然后再用它们去替换 func 那一部分的对应符号. c... 只是表明它对应所有剩下的符号, 而不同于 C 函数中不定量参数的表示法.

一句话, #define 不涉及语法, 只替换格式
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-9-30 09:57:44 | 显示全部楼层
OK
谢谢各位的讲解,对我是够用了。

如果有哪位大师要细细讲解的话,我也不介意,呵呵
回复 支持 反对

使用道具 举报

发表于 2007-8-29 22:48:50 | 显示全部楼层
我尚有一个案例想要请教:
#if defined(_MSC_VER)
这个宏似乎和OS有关,但是我没这方面经验,搞不懂。
回复 支持 反对

使用道具 举报

发表于 2007-8-31 21:15:21 | 显示全部楼层
这两者是一样的.

[URL="http://gcc.gnu.org/onlinedocs/cpp/Defined.html"][/URL]

The special operator defined is used in `#if' and `#elif' expressions to test whether a certain name is defined as a macro. defined name and defined (name) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise. Thus, #if defined MACRO is precisely equivalent to #ifdef MACRO.

defined is useful when you wish to test more than one macro for existence at once. For example,

     #if defined (__vax__) || defined (__ns16000__)

would succeed if either of the names __vax__ or __ns16000__ is defined as a macro.

Conditionals written like this:

     #if defined BUFSIZE && BUFSIZE >= 1024

can generally be simplified to just #if BUFSIZE >= 1024, since if BUFSIZE is not defined, it will be interpreted as having the value zero.

If the defined operator appears as a result of a macro expansion, the C standard says the behavior is undefined. GNU cpp treats it as a genuine defined operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option -pedantic, since other compilers may handle it differently.
回复 支持 反对

使用道具 举报

发表于 2007-8-31 21:16:44 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表