当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)
C陷阱与缺陷 第8章 建议与答案 8.2 答案
(七)《数电》——CMOS与TTL门电路
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
方舟生存进化是什么游戏?好不好玩
Taobao's API to get the list of shipping addresses
C Expert Programming Preface
JS hoisting: how to break the chain of Promise calls
R语言 pca主成分分析的主要方法
空间数据库开源路,超图+openGauss风起禹贡
Jmeter实战 | 同用户重复并发多次抢红包
Record the first PR to an open source project
C专家编程 第1章 C:穿越时空的迷雾 1.1 C语言的史前阶段
LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
【接口测试】JMeter调用JS文件实现RSA加密
那些关于DOM的常见Hook封装(二)
Pytorch框架学习记录10——线性层
Go Atomic
如何让定时器在页面最小化的时候不执行?
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion









