当前位置:网站首页>Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
2022-08-01 21:16:00 【weixin_Guest time】
A.3 stdargs.h: ANSI version of varargs.h
Functions with variadic argument lists whose first argument's type is virtually unchanged from call to call.The main difference between varargs.h and stdargs.h comes from this fact.A function like printf can determine the type of its second argument by examining its first argument.However, we cannot find any information from the parameter list that allows us to determine the type of the first parameter.Therefore, functions using stdarg.h must have at least one argument of fixed type, which can be followed by an unknown number of arguments of unknown type.
void error(char *, ...);
Use the function of stdarg.h to directly declare its fixed parameters, and use the last fixed parameter as the parameter of the va_start macro, that is, use the fixed parameter as the basis for the variable parameter.
The definition of error is as follows:
#include
#include
void error(char *format, ...) {
va_list ap;
va_start(ap, format);
fprintf(stderr, "error: ");
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
In this example, there is no need to use the va_arg macro, so the format string here belongs toFixed part of the parameter list.
#include
int printf(char *format, ...) {
va_list ap;
int n;
va_start(ap, format);
n = vprintf(format, ap);
va_end(ap);
Return n;
}
边栏推荐
- Kubernetes 如何实现组件高可用
- 和我一起写一个音乐播放器,听一首最伟大的作品
- 方舟:生存进化官服和私服区别
- win10版本1803无法升级1903系统如何解决
- Transplant MQTT source code to STM32F407 development board
- C专家编程 第1章 C:穿越时空的迷雾 1.2 C语言的早期体验
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
- 【中文树库标记---CTB】
- JS hoisting: how to break the chain of Promise calls
猜你喜欢
随机推荐
C陷阱与缺陷 第8章 建议与答案 8.1 建议
位运算简介
牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
方舟生存进化是什么游戏?好不好玩
空间数据库开源路,超图+openGauss风起禹贡
C陷阱与缺陷 第5章 库函数 5.5 库函数signal
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
Pytorch框架学习记录8——最大池化的使用
R语言进行相关的操作
C专家编程 前言
列表页常见的 hook 封装
C Pitfalls and Defects Chapter 7 Portability Defects 7.8 Size of Random Numbers
Pytorch框架学校记录11——搭建小实战完整细节
方舟开服需要知道的那些事
Shell编程之条件语句
一个关于操作数据库的建议—用户密码
技术栈概览
Pytorch框架学习记录13——利用GPU训练
左旋氧氟沙星/载纳米雄黄磁性/As2O3磁性Fe3O4/三氧化二砷白蛋白纳米球
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?