C语言中宏的使用
来源:互联网 发布:厦门软件培训 编辑:程序博客网 时间:2024/05/16 12:59
C语言中宏的使用(#,##,do…while(0)宏)
1.预定义宏的使用__FILE__,__FUNCTION__,__LINE__。
- #include <stdio.h>
- void fun(void)
- {
- char v1;
- short v2;
- int v3;
- long v4;
- printf("v1: %x\n",&v1);
- printf("v2: %x\n",&v2);
- printf("v3: %x\n",&v3);
- printf("v4: %x\n",&v4);
- printf("file is :%s,\tfunction is: %s,\tline is :%d\n",__FILE__,__FUNCTION__,__LINE__);
- }
- int main()
- {
- fun();
- printf("file is :%s,\tfunction is: %s,\tline is :%d\n",__FILE__,__FUNCTION__,__LINE__);
- return 0;
- }
2.#宏字符串化
- #include<stdio.h>
- #define dprintf(expr) printf("<main>%s=%d\n",#expr,expr)
- int main()
- {
- int x=100;
- int y=2;
- dprintf(x/y);//#expr相当于x/y expr是x/y的运算结果 #是字符串化操作
- dprintf(x+y);
- dprintf(x+y+2);
- return 0;
- }
3.##连接字符串
- #include<stdio.h>
- #define test(x) test ##x
- void test1(int a)
- {
- printf("Test 1 integer: %d \n",a);
- }
- void test2(char *s)
- {
- printf("Test 2 String: %s \n",s);
- }
- int main ()
- {
- test(1)(100); //test(1)等价于test; test##1等价于test1;##连接字符
- test(2)("hello");
- return 0;
- }
4.do...while(0)宏
- #include<stdio.h>
- #define hello(str) do{\
- printf("Hello:%s\n",str);\
- }while(0)
- int main()
- {
- int m=0;
- if(m)
- {
- hello("hello true");
- }
- else
- hello("hello false");
- system("pause");
- return 0;
- }
0 0
- C语言中宏的使用
- C语言中宏的使用
- c 语言中宏的使用技巧
- c语言中宏的使用
- C 语言中宏的使用
- C语言中宏的使用
- C语言中宏的使用
- C语言中宏的使用
- C语言中使用空的宏定义的作用
- c语言宏定义中##连接符和#的使用
- 在C语言的宏中使用类型关键字
- C语言宏定义中do while(0)的使用
- 软件工程中调试宏的使用(C语言)
- C语言中const的使用
- c语言中使用环境变量的技巧
- C语言中使用环境变量的技巧
- C语言中使用环境变量的技巧
- C语言中va函数的使用
- VPN,解除封锁将hi5,如何获得对将hi5在学校,将hi5代理服务器
- router map
- VPN进行数据保护和网络安全
- bc命令详解
- 文件描述符
- C语言中宏的使用
- ios tableView滑动动画
- hdu2833 WuKong
- IOS 条形码技术
- DialogBoxParamA
- mysql物理还原
- mysql主从同步常见异常及恢复方法
- VPN的解释 - 简化的VPN的基础知识
- POJ 1679 The Unique MST 次小生成树