当前位置:网站首页>附录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;
}
边栏推荐
- system collection
- ahooks 是怎么处理 DOM 的?
- 任务调度线程池基本介绍
- 面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
- Digital twin Beijing the imperial palace, yuan universe is the process of tourism
- Hiking, cured my mental internal friction
- 通俗解释:什么是临床预测模型
- 织梦发布文章提示body has not allow words错误
- Different operating with different locks, rounding
- 有点奇怪!访问目的网址,主机能容器却不行
猜你喜欢

外骨骼机器人(七):标准步态数据库

【Dart】dart之mixin探究
![[Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18](/img/f3/a8813759e5b4dd4b4132e65672fc3f.png)
[Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18

excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列

Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)

PyTorch笔记 - Attention Is All You Need (2)

KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System

【Social Media Marketing】How to know if your WhatsApp is blocked?

Qt设置应用程序开机自启 解决设置失败原因

R语言 数据的关系探索
随机推荐
R语言 数据的关系探索
Remove 360's detection and modification of the default browser
使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性
New graduate students, great experience in reading English literature, worthy of your collection
如何用Chrome编辑以及调试代码
Goroutine Leaks - The Forgotten Sender
vant实现Select效果--单选和多选
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
Imitation cattle forum project
Multithreaded producers and consumers
通俗解释:什么是临床预测模型
30+的女性测试人面试经验分享
Pytorch框架学习记录12——完整的模型训练套路
织梦模板加入php代码
JS提升:如何中断Promise的链式调用
案例:MySQL主从复制与读写分离
Failed to re-init queues : Illegal queue capacity setting (abs-capacity=0.6) > (abs-maximum-capacity
Goroutine Leaks - The Forgotten Sender
线上问题排查常用命令,总结太全了,建议收藏!!
myid file is missing