#define的继续讨论

Plux posted @ 2010年12月31日 02:39 in C/C++ with tags c/c++ #define 宏定义 , 1903 阅读

正如上次所说的,#define可以定义语言结构,例如:

#include <stdio.h>

#define Dear(x)  void main(x) {
#define she      int argc, int *argv[]
#define I        int i;
#define love     for(i=0;i<99;i++)
#define you      printf("I love you!\n");
#define by       ;
#define name      }

Dear(she) 
	I love you
by name

可以把一段原来很普通的代码变得“优雅”。当然,C语言在宏命令处理方面肯定没有其它宏命令处理强,而且也容易出错。

正如代码所说的,可以定义没有“main()”的代码。

Avatar_small
逍遥云 说:
Dec 31, 2010 06:03:21 PM

define的用法我觉得还是着重在##, # 链接符的用法, 你这个纯是好玩了.
如:

#include <stdio.h>
#include <string.h>

#define test(a, b) \
int a##1 = b;\
char tmpstr[30];\
strcpy(tmpstr, "define:"#b);

int main(int argc, char** argv)
{
test(num, 100);
printf("test info:%d, %s", num1, tmpstr);
return 0;
}

Avatar_small
Plux 说:
Dec 31, 2010 06:11:48 PM

@逍遥云: 嗯,的确,我正在学习C语言中…

Avatar_small
Plux 说:
Jan 07, 2011 01:51:29 AM

@Kflayca: It should be "int main"...


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter