当前位置:网站首页>附录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;
}
边栏推荐
猜你喜欢

Pytorch框架学习记录9——非线性激活

Interview Blitz 70: What are sticky packs and half packs?How to deal with it?

【Kaggle】House Prices

2022年秋招,软件测试开发最全面试攻略,吃透16个技术栈

STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障

R语言 数据的关系探索

Remove 360's detection and modification of the default browser
![[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room](/img/c2/456eec59930321b4f088020905c246.jpg)
[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room

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

用户身份标识与账号体系实践
随机推荐
技能大赛训练:A部分加固题目
15 分钟带你入门 Grafana
案例:MySQL主从复制与读写分离
系统收集集
98. Embedded controller EC actual combat EC development board development completed
Failed to re-init queues : Illegal queue capacity setting (abs-capacity=0.6) > (abs-maximum-capacity
密码学的基础:X.690和对应的BER CER DER编码
算法---解码方法(Kotlin)
通俗解释:什么是临床预测模型
如何封装 cookie/localStorage/sessionStorage hook?
图的邻接矩阵存储
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
StringTable Detailed String Pool Performance Tuning String Concatenation
如何让定时器在页面最小化的时候不执行?
微信小程序云开发|个人博客小程序
MySQL Syntax Basics
SIPp installation and use
R语言进行相关的操作
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
外骨骼机器人(七):标准步态数据库