先上一段代码:

#include<cstdarg>
#include<iostream>
#include<string>
using namespace std;
void error(char* format,...){//至少要有一个参数,后边的...表示参数可变
va_list ap;
int d,flag;
char c, *s;
va_start(ap,*format);//从args参数开始后面都是可变参数,va_start,va_end都为宏
while (*format){
switch (*format) {
case 's': /* string */
if(!flag) break;
s = va_arg(ap, char *);
cout<<s;flag=;
break;
case 'd': /* int */
if(!flag) break;
d = va_arg(ap, int);
cout<<d;flag=;
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
if(!flag) break;
c = (char) va_arg(ap, int);
cout<<c;flag=;
break;
case '%':
flag=;
break;
default:
putchar(*format);
}
format++; }
putchar('\n');
/*for(i=0;i<args;i++){//遍历所有其他参数
//printf("%d",va_arg(ap,int));
}*/
va_end(ap);//destroy va_list;
}
int main(){
char s[]="rich";
error("I am %s,I have %d %c",s,,'$');
//getchar();
return ;
}
其中error()函数像printf一样打印出格式化字符串。
#include <stdarg.h>
头文件中定义了一下一些宏,注意不是函数
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);
有趣的是va_arg宏,每次处理后ap指向下一个参数,请看man手册:
va_arg()
The va_arg() macro expands to an expression that has the type and value
of the next argument in the call. The argument ap is the va_list ap
initialized by va_start(). Each call to va_arg() modifies ap so that
the next call returns the next argument. The argument type is a type
name specified so that the type of a pointer to an object that has the
specified type can be obtained simply by adding a * to type. The first use of the va_arg() macro after that of the va_start() macro
returns the argument after last. Successive invocations return the
values of the remaining arguments. If there is no next argument, or if type is not compatible with the
type of the actual next argument (as promoted according to the default
argument promotions), random errors will occur. If ap is passed to a function that uses va_arg(ap,type) then the value
of ap is undefined after the return of that function.

C语言之函数可变参数的更多相关文章

  1. C语言中函数可变参数解析

    大多数时候,函数中形式参数的数目通常是确定的,在调用时要依次给出与形式参数对应的所有实际参数.但在某些情况下希望函数的参数个数可以根据需要确定.典型的例子有 大家熟悉的函数printf().scanf ...

  2. 【C/C++开发】C语言实现函数可变参数

    函数原型: int printf(const char *format[,argument]...)        返 回 值: 成功则返回实际输出的字符数,失败返回-1.  函数说明:        ...

  3. C语言函数可变参数列表

    C语言允许使用可变参数列表,我们常用的printf函数即为可变参数函数,C标准库提供了stdarg.h为我们提供了这方面支持:该头文件提供了一些类型和宏来支持可变参数列表,包括类型va_list,宏v ...

  4. C 函数可变参数

    C 函数可变参数 C 语言中用 ... 表示可变参数,例如: void fun(int x ...) 头文件 cstdarg.h 中包含可变参数类型va_list和处理可变参数的三个宏: va_lis ...

  5. C语言中的可变参数-printf的实现原理

    C语言中的可变参数-printf的实现原理 在C/C++中,对函数参数的扫描是从后向前的.C/C++的函数参数是通过压入堆栈的方式来给函数传参数的(堆栈是一种先进后出的数据结构),最先压入的参数最后出 ...

  6. c#编程基础之函数可变参数

    可变参数:int sum (params int[] values)int sum (string name,params int[] values) 注意:params参数必须是形参表中的最后一个参 ...

  7. PHP函数可变参数列表的具体实现方法介绍

    PHP函数可变参数列表可以通过_get_args().func_num_args().func_get_arg()这三个函数来实现.我们下面就对此做了详细的介绍. AD:2014WOT全球软件技术峰会 ...

  8. Python函数可变参数*args及**kwargs详解

    初学Python的同学们看到代码中类似func(*args, **kwargs)这样的函数参数定义时,经常感到一头雾水. 下面通过一个简单的例子来详细解释下Python函数可变参数*args及**kw ...

  9. C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) .

    我们在C语言编程中会遇到一些参数个数可变的函数,一般人对它的实现不理解.例如Printf(): Printf()函数是C语言中非常常用的一个典型的变参数函数,它 的原型为: int printf( c ...

随机推荐

  1. BZOJ 1026 【SCOI2009】 windy数

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? I ...

  2. Logging的这点小事

    真正做项目,才发觉Logging的好处和学问.自己胡搞的时候,常常使用System.out.println作为输出. 但实际的项目,尤其是library比较多的时候,好好配置好Logging,才能在后 ...

  3. 数据库MySQL与Oracle的一些去O注意项

    一.oracle递归查询语句start with ...connect by prior ① 给你一张表,表里面有主键id,以及该项的父节点parent_id,查询出该表中所有的父子关系节点树? Or ...

  4. Java:注解(元数据)

    初识Java注解 所谓的元数据是指用来描述数据的数据,可能刚听到元数据的时候你会有点陌生,其实任何一个使用过struts或者hibernate的开发人员都在不知不觉中使用元数据,更通俗一点来说元数据是 ...

  5. 外网不能访问部署在虚机的NodeJs网站(80端口)

    外网能访问部署在虚机的NodeJs网站需注意如下: 在管理门户上配置端点(Http 80->80) 在虚机中的防火墙入站规则中增加应用程序Node.exe的允许规则 启动NodeJs的侦听进程时 ...

  6. nios II--实验1——hello_world软件部分

    hello_world 软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for ...

  7. JavaScript学习笔记-自定义滚动条

    这是一个基本实现思路,如果有新手和我一样没什么事,喜欢瞎研究话,可以参考下. 一.Html <div class="scroll_con"> <div class ...

  8. javascript 连等赋值问题(这是从SegmentFault转过来的一个问题)

    var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);// - ...

  9. 60-chmod 修改文件的权限

    修改文件的权限 chmod [options] who operator permission file-list (符号模式) chmod [options] mode file-list (绝对模 ...

  10. C++11异常处理 noexcept

    1.简介 在C语言中,如果程序的运行出现异常.错误,我们想提供方案处理这些异常时,我们面临许多问题,如: (1)C语言没有提供统一(标准)的方式来处理错误: (2)无法保证错误会被正确的处理: (3) ...