当前位置:网站首页>C language__ VA_ ARGS__ Usage of
C language__ VA_ ARGS__ Usage of
2022-06-29 13:41:00 【fengwang0301】
C Detailed explanation of language variable parameter function
0 Preface
In the course of debugging , Sometimes a custom print is used , This is what will be used ##__VA_ARGS__, Let's talk about it in detail .
1 __VA_ARGS__ Usage of
When customizing printing , Use variable parameters , use ... Can represent variable parameters , as follows :
#include <stdio.h>
#define LOG1(...) printf(__VA_ARGS__)//... Represents a variable parameter ,__VA_ARGS__ Will be ... The value of is copied here
int main(int argc, char** argv)
{
char *str = "test __VA_ARGS__";
int num = 10086;
LOG1("this is test __VA_ARGS__\r\n");
LOG1("this is test __VA_ARGS__:%s, %d\r\n", str, num);
return 0;
}
The results are as follows :
this is test __VA_ARGS__
this is test __VA_ARGS__:test __VA_ARGS__, 10086
2 ##__VA_ARGS__ Usage of
##__VA_ARGS__ prefix ## The role of is : When the number of variable parameters is 0 when , there ## You can put the superfluous in front "," Get rid of , Otherwise, there will be compilation errors .
When you want to add time to the custom debug information 、 The number of rows and other information , What should be done ? Write the correct usage first :
#include <stdio.h>
#define LOG3(fmt, ...) printf("<%s:%s>:"fmt"\r\n", __FILE__, __FUNCTION__, ##__VA_ARGS__)
int main(int argc, char** argv)
{
char *str = "test __VA_ARGS__";
int num = 10086;
LOG3("this is test __VA_ARGS__");
LOG3("this is test __VA_ARGS__:%s, %d", str, num);
LOG3();
return 0;
}
Print as follows :
<main.c:main>:this is test __VA_ARGS__
<main.c:main>:this is test __VA_ARGS__:test __VA_ARGS__, 10086
<main.c:main>:
Then make a comparison : If you use __VA_ARGS__, When used , The parameter is empty. , Compiler error . as follows :
#include <stdio.h>
#define LOG2(fmt, ...) printf("<%s:%s>:"fmt"\r\n", __FILE__, __FUNCTION__, __VA_ARGS__)
int main(int argc, char** argv)
{
LOG2();// Don't pass parameters , Compiler error
LOG2("this is test __VA_ARGS__");// Compiler error
LOG2("this is test __VA_ARGS__:%s, %d", str, num);// Normal compilation
return 0;
}
Error report after compilation :
main.c: In function ‘main’:
main.c:3:96: error: expected expression before ‘)’ token
3 | #define LOG2(fmt, ...) printf("<%s:%s>:"fmt"\r\n", __FILE__, __FUNCTION__, __VA_ARGS__)
| ^
main.c:6:5: note: in expansion of macro ‘LOG2’
6 | LOG2();// Don't pass parameters , Compiler error
| ^~~~
main.c:3:96: error: expected expression before ‘)’ token
3 | #define LOG2(fmt, ...) printf("<%s:%s>:"fmt"\r\n", __FILE__, __FUNCTION__, __VA_ARGS__)
| ^
main.c:7:5: note: in expansion of macro ‘LOG2’
7 | LOG2("this is test __VA_ARGS__");
| ^~~~
analysis :
- The first 6 Line has no parameters , Macro definition
LOG2(fmt,...)After deployment ,__VA_ARGS__It's empty. , At this timeprintfThere is one left,, Inevitably, compilation fails ; - The first 7 Although the line passed in a string , But the string is assigned to
LOG2(fmt,...)The first parameter offmt, After the grand exhibition ,printfThere is one left behind,, So compile error .
summary
##__VA_ARGS__ in ## The function of is to get rid of the superfluous ,, When using custom printing , Recommended use of 2 The way of small measure .
Welcome to comment area message exchange !
边栏推荐
- 【系统设计】邻近服务
- Write it down once Net analysis of a property management background service stuck
- Uncover the secret! Pay attention to those machines under the membership system!
- Mirror vulnerability scanner: trivy
- 如何让 Dapper 支持 DateOnly 类型
- 开户可以在网上开么?能安全吗
- Huawei machine learning service speech recognition function enables applications to paint "sound" and color
- 请问老师炒期货怎么设定安全线和安全边际?
- mysql调优
- 项目管理复习题
猜你喜欢

C语言内存函数

Weserver publishing map service

System.currentTimeMillis() 和 System.nanoTime() 哪个更快?大部分人都会答错!

从零搭建Pytorch模型教程(四)编写训练过程--参数解析

华为机器学习服务语音识别功能,让应用绘“声”绘色

C language memory function

What if the excel table exported from the repair record is too large?

Windbg常用命令详解

Lecun published a research plan for the next decade: AI autonomous intelligence with a 62 page paper

weserver发布地图服务
随机推荐
matplotlib的imshow函数显示灰度图像要设置vmin和vmax2个参数
从零搭建Pytorch模型教程(四)编写训练过程--参数解析
koa2+better-sqlite3实现增删改查
如何统计项目代码(比如微信小程序等等)
Check yaml file security configuration: kubesec
Cvpr2022 | reexamine pooling: your receptive field is not the best
Proteus simulation of buck switching power supply based on 51 single chip microcomputer
安装terraform-ovirt插件为ovirt提供自动化管理
思科模拟器简单校园网设计,期末作业难度
WinDbg common commands
LeCun用62页论文公布未来十年研究计划:AI自主智能
GBase8s数据库遇到951错误是什么原因?
Windbg常用命令详解
@Table爆红
weserver發布地圖服務
*打卡算法*LeetCode 146. LRU 缓存 算法解析
Lecun published a research plan for the next decade: AI autonomous intelligence with a 62 page paper
Repoptimizer: it's actually repvgg2
商品搜索引擎—推荐系统设计
The scale of 360 digital new energy special products exceeded 6billion