当前位置:网站首页>附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
2022-08-01 21:00:00 【weixin_客子光阴】
A.3 stdargs.h:ANSI版的varargs.h
具有可变参数列表的函数,它们的第1个参数的类型在每次调用时实际上都是不变的。varargs.h和stdargs.h的主要区别就来自于这一事实。类似printf这样的函数,可以通过检查它的第一个参数,来确定它的第2个参数的类型。但是,从参数列表中我们找不到让任何信息用来确定第1个参数的类型。因此,使用stdarg.h的函数必须至少有一个固定类型的参数,后面可以跟一组未知数目、未知类型的参数。
void error(char *, ...);
使用stdarg.h的函数直接声明其固定参数,把最后一个固定参数作为va_start宏的参数,即以固定参数作为可变参数的基础。
error的定义如下所示:
#include <stdio.h>
#include <stdarg.h>
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);
}
本例中,无须使用va_arg宏,因此此处格式字符串属于参数列表的固定部分。
#include <stdarg.h>
int printf(char *format, ...) {
va_list ap;
int n;
va_start(ap, format);
n = vprintf(format, ap);
va_end(ap);
return n;
}
边栏推荐
猜你喜欢
随机推荐
What is the difference between a utility model patent and an invention patent?Understand in seconds!
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
Questions I don't know in database kernel interview(1)
封装一个管理 url 状态的 hook
Interpretation of the meaning of each dimension of two-dimensional, three-dimensional, and four-dimensional matrices
线程池处理异常的方法
Failed to re-init queues : Illegal queue capacity setting (abs-capacity=0.6) > (abs-maximum-capacity
Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
1374. 生成每种字符都是奇数个的字符串 : 简单构造模拟题
案例:MySQL主从复制与读写分离
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
PyTorch笔记 - Attention Is All You Need (2)
[Personal work] Wireless network image transmission module
Imitation cattle forum project
Pytorch框架学习记录13——利用GPU训练
人工智能可信安全与评测
Telnet弱口令渗透测试
【Untitled】
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]





![[Personal work] Wireless network image transmission module](/img/64/c0cec74692df7ca05c1a5317e21c9d.png)



