c: macro define 复杂的宏定义
一、源码
[root@rockylinux tmp]# cat macro_define.c
/**
* 宏命定义的注意事项:
* 1、带有参数的宏,参数使用时需要写在"()"之中,这样在宏展开时不会改变设计时的运算级别,保证结果正确;
* 举例:#define MAX_INT(x,y) (x)>(y)?(x):(y)
* 2、多行合并为一行(代码换行):当“宏定义”内容超过一行时,在语句末尾用"\"换行;在“空行的末尾”也要添加"\";
*
* 3、多行代码,可以使用"{}"和"()"。
*
*
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX_USER() 20
#define SAY(x,y) \
{\
printf("SAY:\t%s\n", (x)); \
printf("SAY:\t%s\n", (y)); \
}
#define BOOK(x,y) {\
printf("book_name=%s, \tbook_price=%s\n", (x), (y));\
}
#define LOOP(x) { \
if((x)<=1){ exit(1);}\
int max = (x);\
for( int i=0; i<max; i++ ) printf("LOOP:\t\tcount=%d\n",i);\
}
void msg()
{
for(int i=0; i<MAX_USER(); i= i + 2)
printf("%d\t\tmax_user=%d,\tcurrent=%d\n", i/2, MAX_USER(), i);
}
int main(int argc, char *argv[])
{
LOOP(10);
BOOK("Gangtie shi zenyang lianchengde", "23.5");
SAY("hello world!", "hello houdini19.");
msg();
return 0;
}
[root@rockylinux tmp]#
[root@rockylinux tmp]#
[root@rockylinux tmp]# ./macro_define
LOOP: count=0
LOOP: count=1
LOOP: count=2
LOOP: count=3
LOOP: count=4
LOOP: count=5
LOOP: count=6
LOOP: count=7
LOOP: count=8
LOOP: count=9
book_name=Gangtie shi zenyang lianchengde, book_price=23.5
SAY: hello world!
SAY: hello houdini19.
0 max_user=20, current=0
1 max_user=20, current=2
2 max_user=20, current=4
3 max_user=20, current=6
4 max_user=20, current=8
5 max_user=20, current=10
6 max_user=20, current=12
7 max_user=20, current=14
8 max_user=20, current=16
9 max_user=20, current=18
[root@rockylinux tmp]#
[root@rockylinux tmp]#
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/16439539.html