printf()有太多args - 合法吗? [英] printf() with too many args -- legal?

查看:91
本文介绍了printf()有太多args - 合法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道传递printf()的参数太少,或者错误的参数

类型调用UB。但是,如果

预期参数的类型正确,那么传递太多参数呢?


例如:


char format1 [] =" foo%s bar%s baz%d" ;;

char format2 [] =" foo%s bar%s no baz here" ;;


char * strvar1 =" one" * strvar2 =" two";

int intvar = 123;


....


printf(some_condition?format1:format2,strvar1,strvar2,intvar);


当some_condition时是假的,printf()会期望两个字符串,但是

会传递两个字符串和一个int。


是的,我知道这可以改写为:


if(some_condition)

printf(format1,strvar1,strvar2,intvar);

else

printf(format2,strvar1,strvar2);


但是,这个结构出现在现有

代码的许多地方,适用于所有当前平台它被移植到了。我想

确保在未来的平台上不会出现问题。


-

+ ------------------------- + -------------------- + - ---------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>

I know that passing printf() too few arguments, or arguments of the wrong
type invokes UB. However, what about passing too many arguments, if the
expected arguments are of the correct type?

For example:

char format1[] = "foo %s bar %s baz %d";
char format2[] = "foo %s bar %s no baz here";

char *strvar1 = "one", *strvar2 = "two";
int intvar = 123;

....

printf( some_condition ? format1 : format2, strvar1, strvar2, intvar );

When "some_condition" is false, printf() will expect two strings, but will
be passed two strings and an int.

Yes, I know this can be rewritten as:

if ( some_condition )
printf(format1,strvar1,strvar2,intvar);
else
printf(format2,strvar1,strvar2);

However, this construct appears in numerous places throughout existing
code, which works on all current platforms it''s ported to. I''d like to
make sure that it won''t cause problems on some future platform.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>

推荐答案

On 2005年6月24日星期五12:58:04 -0400,Kenneth Brody写道:
On Fri, 24 Jun 2005 12:58:04 -0400, Kenneth Brody wrote:
我知道传递printf()的参数太少,或者参数错误
类型调用UB。但是,如果
预期参数的类型正确,那么传递太多参数呢?
I know that passing printf() too few arguments, or arguments of the wrong
type invokes UB. However, what about passing too many arguments, if the
expected arguments are of the correct type?




那没关系,它不是必要的变量参数函数读取整个变量参数列表



劳伦斯



That''s fine, it isn''t necessary for a variable argument function to read
the whole variable argumet list.

Lawrence




" Kenneth Brody" <柯****** @ spamcop.net>在消息中写道

news:42 *************** @ spamcop.net ...

"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:42***************@spamcop.net...
我知道传递printf( )太少的参数,或错误的
类型的参数调用UB。但是,如果
预期参数的类型正确,那么传递太多参数呢?
如果他们不被预期,他们就不能是正确的类型!

但这对你来说实际上是个好消息...其他论据不会是

被视为该函数仅在请求转换时请求参数格式字符串中的转换

说明符。


Mark
例如:

char format1 [] =" foo%s bar%s baz%d" ;;
char format2 [] =" foo%s bar%s no baz here" ;;

char * strvar1 =" one" * strvar2 =" two";
int intvar = 123;

....

printf(some_condition?format1:format2,strvar1,strvar2,intvar);

当some_condition时是假的,printf()会期望两个字符串,但是会传递两个字符串和一个int。

是的,我知道这可以改写为:
if(some_condition)
printf(format1,strvar1,strvar2,intvar);

printf(format2,strvar1,strvar2);

然而,这个结构在现有的代码中出现在许多地方,这些代码适用于它所移植到的所有当前平台。我想确保在未来的某个平台上不会出现问题。

-
+ ---------- --------------- + -------------------- + ------------- ---------------- +
| Kenneth J. Brody | www.hvcomputer.com |
|
| kenbrody / at\spamcop.net | www.fptech.com | #include
< std_disclaimer.h> |
+ ------------------------- + ------------------ - + ----------------------------- +
不要给我发电子邮件:< mailto :Th ************* @ gmail.com>
I know that passing printf() too few arguments, or arguments of the wrong
type invokes UB. However, what about passing too many arguments, if the
expected arguments are of the correct type? They can''t be the correct type if they''re not expected!
But that''s actually good news for you... additional arguments will not be
seen as the function only requests arguments as it encounters conversion
specifiers in the format string.

Mark

For example:

char format1[] = "foo %s bar %s baz %d";
char format2[] = "foo %s bar %s no baz here";

char *strvar1 = "one", *strvar2 = "two";
int intvar = 123;

....

printf( some_condition ? format1 : format2, strvar1, strvar2, intvar );

When "some_condition" is false, printf() will expect two strings, but will
be passed two strings and an int.

Yes, I know this can be rewritten as:

if ( some_condition )
printf(format1,strvar1,strvar2,intvar);
else
printf(format2,strvar1,strvar2);

However, this construct appears in numerous places throughout existing
code, which works on all current platforms it''s ported to. I''d like to
make sure that it won''t cause problems on some future platform.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com |
|
| kenbrody/at\spamcop.net | www.fptech.com | #include
<std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>





Kenneth Brody写道:


Kenneth Brody wrote:
我知道传递printf()的参数太少,或者错误的
类型的参数调用UB。但是,如果
预期参数的类型正确,那么传递太多参数呢?
I know that passing printf() too few arguments, or arguments of the wrong
type invokes UB. However, what about passing too many arguments, if the
expected arguments are of the correct type?




Legal。 7.19.6.1/2:如果格式耗尽而

参数仍然存在,则评估多余的参数(总是为

),否则将被忽略。


-
Er ********* @ sun.com



Legal. 7.19.6.1/2: "If the format is exhausted while
arguments remain, the excess arguments are evaluated (as
always) but are otherwise ignored."

--
Er*********@sun.com


这篇关于printf()有太多args - 合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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