[ASNI C] [常用宏定义] [define技巧]
2024-10-14 10:40:09
1. 打印变量名及其值
#define Inquire(var, mode) fprintf(stdout, #var" = "#mode". \n", var)
// Usage
PSTR $PATH$ = "C:\\windows\\system32;.";
Inquire($PATH$, "%s");
// Output
// $PATH$ = "c:\windows\system32;.".
2.可变参宏的使用
void _Log(const char *format, ...);
#define Log(format, ...) _Log(format, ##__VA_ARGS__) //__VA_ARGS__是C99提供的特性可变宏参数,##用于当没有多余参数时删除format之后的','避免编译错误
#include <stdarg.h>
#include <stdio.h> static int i = ;
void _Log(const char *format, ...){
va_list list = ; // NULL
va_start(list, format);
{
fprintf(stdout, "==Log %d== ", ++i);
vfprintf(stdout, format, list);
fprintf(stdout, "\n");
}
va_end(list);
}
3.断言
void _Assert(const char *info, const char *func, int line);
#define Assert(x) if(!(x)) _Assert("断言失败!文件:" __FILE__ ",函数:%s,行:%d--\"" #x "\".", __FUNCTION__, __LINE__)
#include <stdio.h> void _Assert(const char *info, const char *func, int line){
fprintf(stderr, info, func, line);
abort();
}
最新文章
- Excel&mdash;&mdash;MATCH函数
- 【leetcode】Roman to Integer
- DataTable Linq Example
- strace命令(收集整理,常看常新)
- zipline tradingcalendar
- cmd命令查看端口和进程信息
- sharepoint2013 新建母板页 新建页面布局 关联母板页和页面布局
- 新一批电子商务解决方案和企业管理应用加入 VM Depot 中国站点
- Oracle百问百答(二)
- HTTP数据包头解析(简单清楚)
- Objective-C Effective 技巧
- ELK系列~nxlog实现多位置文件的收集
- struts2 添加请求后缀的3种方式
- Intellij Idea 2018常用快捷键总结
- (九)逻辑运算,order by,desc
- Django Model模型的实战操作笔记
- Pycharm快捷键大全(windows + Mac)
- git difftool和mergetool图形化
- TP5 首页导航一级和二级分类
- nio入门教程
热门文章
- [AH2017/HNOI2017]礼物(FFT)
- NOIP2013花匠(波动序列)
- CodeForces - 95B(DFS)
- web字体的设置
- 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)
- QML学习笔记(四)-TabView-竖直方向
- JS判断手机还是电脑访问网站
- 向redis中添加删除list列表
- template specifiers not specified in declaration of ‘template<;class Key>; class hash’
- session/cookie/token