当前位置:网站首页>附录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;
}
边栏推荐
- LinkedList源码分享
- [Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
- Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)
- 技能大赛训练:A部分加固题目
- 织梦模板加入php代码
- 通俗解释:什么是临床预测模型
- 响应式织梦模板清洁服务类网站
- MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
- tiup mirror genkey
- Remove 360's detection and modification of the default browser
猜你喜欢
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
Digital twin Beijing the imperial palace, yuan universe is the process of tourism
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)
Hiking, cured my mental internal friction
LTE time domain and frequency domain resources
MySQL Syntax Basics
Pytorch框架学习记录9——非线性激活
30+的女性测试人面试经验分享
To promote energy conservation institute 】 【 the opinions of the agricultural water price reform
Remove 360's detection and modification of the default browser
随机推荐
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
tiup mirror clone
线上问题排查常用命令,总结太全了,建议收藏!!
iptables的使用简单测试
Protocol Buffer 使用
Go 语言中常见的坑
虚拟机的IP地址自动变为127.0.0.1
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)
Different operating with different locks, rounding
Where should I prepare for the PMP exam in September?
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
Interview assault 70: what is the glue bag and a bag?How to solve?
进行交互或动画时如何选择Visibility, Display, and Opacity
2022年秋招,软件测试开发最全面试攻略,吃透16个技术栈
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
【Kaggle】House Prices
Redis does check-in statistics
Fork/Join线程池
idea实用快捷键合集——持续更新