当前位置:网站首页>附录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;
}
边栏推荐
猜你喜欢
[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room
Fork/Join线程池
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
乐观锁批量跟新 纯SQL
面试突击70:什么是粘包和半包?怎么解决?
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障
4.1 配置Mysql与注册登录模块
】 【 nn. The Parameter () to generate and why do you want to initialize
函数(二)
Imitation cattle forum project
随机推荐
iptables的使用简单测试
MySQL语法基础
Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
技能大赛训练:A部分加固题目
tiup mirror init
Different operating with different locks, rounding
string
Protocol Buffer 使用
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
通俗解释:什么是临床预测模型
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
Interpretation of the meaning of each dimension of two-dimensional, three-dimensional, and four-dimensional matrices
"No title"
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
WeChat applet cloud development | personal blog applet
Remove 360's detection and modification of the default browser
LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
Internet使用的网络协议是什么
2022年秋招,软件测试开发最全面试攻略,吃透16个技术栈