当前位置:网站首页>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;
}
边栏推荐
- 相亲模型与有限状态机
- C语言之字符串函数二
- 正则表达式
- C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
- JS hoisting: how to break the chain of Promise calls
- C pitfalls and pitfalls Chapter 8 Suggestions and answers 8.2 Answers
- 磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
- 线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- JS提升:手写发布订阅者模式(小白篇)
猜你喜欢
随机推荐
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
R语言 pca主成分分析的主要方法
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.1 The Prehistoric Phase of the C Language
记录第一次给开源项目提 PR
Pytorch框架学习记录13——利用GPU训练
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
C专家编程 序
2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
C陷阱与缺陷 第8章 建议与答案 8.1 建议
C陷阱与缺陷 第5章 库函数 5.5 库函数signal
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
LeetCode
和我一起写一个音乐播放器,听一首最伟大的作品
C专家编程 第1章 C:穿越时空的迷雾 1.1 C语言的史前阶段
淘宝获取收货地址列表的 API
LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
响应式织梦模板美容整形类网站
人工智能可信安全与评测
C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子