惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
N
News | PayPal Newsroom
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
O
OpenAI News
博客园 - Franky
M
MIT News - Artificial intelligence
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
S
Security @ Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Hacker News: Front Page
TaoSecurity Blog
TaoSecurity Blog
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
博客园 - 【当耐特】
C
Cisco Blogs
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
IT之家
IT之家
Recent Announcements
Recent Announcements
Cyberwarzone
Cyberwarzone
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
AI
AI
爱范儿
爱范儿
S
Schneier on Security
Google DeepMind News
Google DeepMind News

博客园 - bullfinch

Notes of "The Unbridged Pentium 4" - Pentium 4 System Overview Notes of "The Unbridged Pentium 4" - Pentium 4 Road Map Notes of "The Unbridged Pentium 4" - Overview of the Processor Role Notes of "Pentium Processor System Architecture" - Pentium Signal Interface (part) Notes of "Pentium Processor System Architecture" - Mutiple Processors and the MESI Model Notes of "Pentium Processor System Architecture" - The Functional Units & Pentium Cache Overview AMD Opteron Architecture related Fedora Core 3 挂载FAT32分区以及中文显示和输入 摄像头设置事件 linux下安装HP NC6000无线网卡(HP W500) C#学习笔记(八) TreeView.AfterCheck和TreeNode.Checked赋值的问题 C#学习笔记(七) C#学习笔记(六) C#学习笔记(五) 图像中密集点群的定位 C#学习笔记(四) 中兴ZXDSL831立式蓝猫自动拨号+NAT+DHCP设置 C#学习笔记(三)
关于变参(zz)
bullfinch · 2005-01-26 · via 博客园 - bullfinch

This section contains an implementation of a minimal version of printf, to
show how to write a function that processes a variable-length argument list
in a portable way. Since we are mainly interested in the argument
processing, minprintf will process the format string and arguments but will
call the real printf to do the format conversions.
The proper declaration for printf is

   int printf(char *fmt, ...)

where the declaration ... means that the number and types of these
arguments may vary. The declaration ... can only appear at the end of an
argument list. Our minprintf is declared as

   void minprintf(char *fmt, ...)


since we will not return the character count that printf does.
The tricky bit is how minprintf walks along the argument list when the list
doesn't even have a name. The standard header <stdarg.h> contains a set of
macro definitions that define how to step through an argument list. The
implementation of this header will vary from machine to machine, but the
interface it presents is uniform.

The type va_list is used to declare a variable that will refer to each
argument in turn; in minprintf, this variable is called ap, for ``argument
pointer.'' The macro va_start initializes ap to point to the first unnamed
argument. It must be called once before ap is used. There must be at least
one named argument; the final named argument is used by va_start to get
started.

Each call of va_arg returns one argument and steps ap to the next; va_arg
uses a type name to determine what type to return and how big a step to
take. Finally, va_end does whatever cleanup is necessary. It must be called
before the program returns.

These properties form the basis of our simplified printf: