当前位置:网站首页>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陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
An online JVM FullGC made it impossible to sleep all night and completely crashed~
这些 hook 更优雅的管理你的状态
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
Suggestions and answer 8.1 C traps and defect chapter 8
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
响应式织梦模板清洁服务类网站
响应式织梦模板美容整形类网站
Telnet弱口令渗透测试
如何让定时器在页面最小化的时候不执行?
Graph adjacency matrix storage
Realize the superposition display analysis of DWG drawing with CAD in Cesium
织梦通过数据库查询调用当前文章的留言
移植MQTT源码到STM32F407开发板上
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
LeetCode·32.最长有效括号·栈·动态规划
JS hoisting: how to break the chain of Promise calls
MySQL Syntax Basics
织梦模板加入php代码









