printf如何处理变量参数? [英] How can printf process variable parameters?

查看:143
本文介绍了printf如何处理变量参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们!!


我们知道定义

函数时函数中的输入参数是固定的但是printf如何处理变量

输入参数的数量?


例如:


1. printf("嘿!怎么样你"); /*没有。 of arguments = 1 * /

2. printf("%d和%d是%d',a,b,c"); / *参数数量= 4 * /


我知道它使用三个宏va_start,va_arg,va_list;但我不知道
知道这些东西是如何运作的......

请解释。


提前致谢! !


Gaurav

Hi friends!!

As we know that the input parameters in a function is fixed when the
function is defined but how does printf processes variable number of
input arguments ?

For example:

1. printf("Hey! how are you"); /*No. of arguments = 1*/
2. printf("Sum of %d and %d is %d",a,b,c"); /* No. of arguments = 4*/

I know it uses three macros va_start, va_arg, va_list ; but i don''t
know how the stuff actually works...
Please explain.

Thanks in advance!!

Gaurav

推荐答案

Googy写道:
Googy wrote:

嗨朋友们!!


我们知道在定义

函数时函数中的输入参数是固定的但是printf如何处理可变数量的

输入参数?
Hi friends!!

As we know that the input parameters in a function is fixed when the
function is defined but how does printf processes variable number of
input arguments ?



[...]

[...]


我知道它使用三个宏va_start,va_arg,va_list;但我不知道
知道这些东西是如何运作的......

请解释。
I know it uses three macros va_start, va_arg, va_list ; but i don''t
know how the stuff actually works...
Please explain.



从这里开始:< http://c-faq.com/varargs/index.html>


最好的C书籍将为您提供可变长度的简要介绍
参数列表。 K& R几次引用它们。

-

clvrmnky< mailto:sp ****** @ clevermonkey.org>


直接回复将被列入黑名单。替换spamtrap以我的名义来

直接与我联系。

Start here: <http://c-faq.com/varargs/index.html>

Most good C books will give you a gentle introduction to variable-length
argument lists. K&R references them a few times.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.


10月1日,18:28,Googy< cool.gaurav.ignit ... @ gmail.comwrote:
On 1 Oct, 18:28, Googy <cool.gaurav.ignit...@gmail.comwrote:

嗨朋友们!!


我们知道函数中的输入参数是固定的

函数已定义,但printf如何处理可变数量的

输入参数?


例如:


1. printf(嘿!你好吗); /*没有。 of arguments = 1 * /

2. printf("%d和%d是%d',a,b,c"); / *参数数量= 4 * /


我知道它使用三个宏va_start,va_arg,va_list;但我不知道
知道这些东西是如何运作的......

请解释一下。
Hi friends!!

As we know that the input parameters in a function is fixed when the
function is defined but how does printf processes variable number of
input arguments ?

For example:

1. printf("Hey! how are you"); /*No. of arguments = 1*/
2. printf("Sum of %d and %d is %d",a,b,c"); /* No. of arguments = 4*/

I know it uses three macros va_start, va_arg, va_list ; but i don''t
know how the stuff actually works...
Please explain.



printf()函数是实现定义的,

因此不需要使用这些va _ ***标记。

这是未定义的行为传递参数

哪些不匹配,在它们的连续类型和

总数中,字段代码格式为字符串,

因此printf()函数可以实现

假设这些代码可用于派生参数的数量和类型,而不是

va_start etcetera。

-

The printf() function is implementation-defined,
and therefore need not use these va_*** tokens.
It is "undefined behaviour" to pass arguments
which do not match, in their successive types and
total number, the field codes in the format string,
therefore the printf() function can be implemented
to assume that these codes can be used to derive
the number and type of the arguments, rather than
va_start etcetera.
--


10月1日下午1点28分,Googy< cool.gaurav.ignit ... @ gmail.comwrote:
On Oct 1, 1:28 pm, Googy <cool.gaurav.ignit...@gmail.comwrote:

嗨朋友们!!


我们知道函数中的输入参数定义

函数时是固定的但是printf如何处理可变数量的

输入参数?


例如:


1. printf(嘿!你好吗); /*没有。 of arguments = 1 * /

2. printf("%d和%d是%d',a,b,c"); / *参数数量= 4 * /


我知道它使用三个宏va_start,va_arg,va_list;但我不知道
知道这些东西是如何运作的......

请解释一下。
Hi friends!!

As we know that the input parameters in a function is fixed when the
function is defined but how does printf processes variable number of
input arguments ?

For example:

1. printf("Hey! how are you"); /*No. of arguments = 1*/
2. printf("Sum of %d and %d is %d",a,b,c"); /* No. of arguments = 4*/

I know it uses three macros va_start, va_arg, va_list ; but i don''t
know how the stuff actually works...
Please explain.



它适用于Magic。


实际上,它适用于编译器编写者选择的任何机制

用于实现va_start等。该机制高度依赖于

传递给

函数的参数的物理排列和位置,以及它们的格式和促销。这就是为什么宏可以提供给b
;底层机制本质上是不可移植的,并且通常模糊不清或错综复杂。


一种机制只适用于堆栈,并且只有在参数为

" push"从右到左进入堆栈。通过这种机制,

进一步正确。你进入参数列表,堆栈上的地址越高。
要找到任何参数,首先需要在堆栈上有一个起点,然后你需要知道多少向上。你要拥有的筹码是多少
,以及为了让你的
对象吸引多少/你必须吸入的筹码。在这样的情况下,被调用的函数可以将最左边的参数的

地址作为基数。或者
堆栈的低点。知道了这个基点的对象有多大,函数

就可以计算堆栈中下一个对象的地址。知道了/那个/对象的大小/(通过预知,或通过解析第一个对象的
的内容),该函数可以计算/的地址br />
next / object。等等。现在,你明白了为什么这个标准会让这个程序员变得不透明,而是给了他va_ *宏呢?它需要详细了解传递

实现工作的基础参数,并且这是大多数应用程序的事情。
程序员不会需要。


It works by Magic.

Actually, it works by whatever mechanism the compiler writer chose to
use to implement va_start, et al. The mechanism is highly dependant on
the physical arrangement and location of arguments passed to a
function, and their format and promotions. That''s why the macros are
made available; the underlying mechanisms are inherently non-portable,
and often obscure or convoluted.

One mechanism works only with a stack, and only if the arguments are
"pushed" onto the stack from right to left. With this mechanism, the
further "right" you go in the argument list, the higher the address on
the stack. To find any argument, you first need a starting point on
the stack, and then you need to know how much "up" the stack you have
to go, and how much /of/ the stack you have to inhale to make your
object up. In a case like that, the called function can take the
address of the left-most argument as the "base" or low point of the
stack. Knowing how big the object at this base point is, the function
can then compute the address of next object on the stack. Knowing the
size of /that/ object (by foreknowledge, or by parsing the contents of
the first object), the function can then compute the address of the /
next/ object. And so on. Now, you see why the standard makes this
opaque to the programmer, and gives him the va_* macros instead? It
needs a detailed understanding of the underlying parameter passing
implementation to work, and that''s something that most application
programmers do not need.



这篇关于printf如何处理变量参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆