一个简单的问题 [英] A quick question

查看:64
本文介绍了一个简单的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下内容:


char s [15] =" Hello,world \ n!";


Are所有以下保证产生相同的输出?


printf("%s",s);

fprintf(stdout,"%s" ,s);

fwrite(s,sizeof(char),sizeof(s)/ sizeof(char) - 1,stdout);


(它这是我特别想知道的最后一个。)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It''s the last of these that I''m specifically wondering about.)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

2004年2月5日星期四,Christopher Benson-Manica写道:
On Thu, 5 Feb 2004, Christopher Benson-Manica wrote:
给定以下内容:

char s [15] =" Hello,world \ n!  

以下是否保证产生相同的输出?

printf("%s",s);
fprintf(stdout,"%s",s);
fwrite(s,sizeof(char),sizeof(s) )/ sizeof(char) - 1,stdout);

(这是我特别想知道的最后一个。)


事实上,标准C89,7.9.6.3 printf函数声明:


printf函数等效于fprintf,参数

stdout插入之前printf的参数。


确保前两个保证产生相同的输出



我能找到的唯一限制是fprintf和printf可能有一个

环境限制,但fwrite没有; 7.9.6.1 fprintf函数,

"任何

单次转换产生的最大字符数的最小值应为509."


PS sizeof char == 1因此你可以把它写成:


fwrite(s,1,sizeof(s) - 1,stdout);

-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It''s the last of these that I''m specifically wondering about.)
The fact that the standard C89, 7.9.6.3 The printf function, states:

The printf function is equivalent to fprintf with the argument
stdout interposed before the arguments to printf."

Makes it fairly certain that the first two are guaranteed to produce the
same output.

The only limitation I can find is that fprintf and printf may have an
environmental limit but fwrite does not; 7.9.6.1 The fprintf function,
"The minimum value for the maximum number of characters produced by any
single conversion shall be 509."

P.S. sizeof char == 1 therefore you could write it as:

fwrite( s, 1, sizeof(s) - 1, stdout );
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.




-

发送电子邮件至:darrell at cs dot toronto dot edu

不要发电子邮件到 vi ************ @ whitehouse .gov


Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写在

news:bv ********** @ chessie.cirr.com:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:bv**********@chessie.cirr.com:
给出以下内容:

char s [15] =" Hello,world \ n!  

以下是否保证产生相同的输出?

printf ("%s",s);
fprintf(stdout,"%s",s);
fwrite(s,sizeof(char),sizeof(s)/ sizeof(char) - 1,stdout);
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );




我以为sizeof(char)是*保证*是一个?


-

- 马克 - >

-



I thought sizeof (char) was *guaranteed* to be one?

--
- Mark ->
--


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
给出以下内容:

char s [15] =" Hello,world \ n!";

以下所有内容均保证生产相同的输出?

printf("%s",s);
fprintf(stdout,"%s",s);


这基本上是printf的定义

fwrite(s,sizeof(char),sizeof(s)/ sizeof(char) - 1,stdout) ;
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
This is basically the definition of printf
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );




是的,从我对标准的阅读。以上所有调用都允许以不同方式失败,但只要完全成功,

输出就会相同。请注意,fwrite必须写输出

,就好像它是用fputc编写的一样。也就是说,对于要写入的每个元素(第三个参数),fputc应写出由第二个参数指定的多个字节

。这些字节中的每一个都应该是连续的
。 (当然,实现只需要表现为 - 如果使用
fputc)。


-

托马斯。



Yup, from my reading of the standard. All call above are allowed to
fail in different ways, but as long as then completely succeed the
output will be the same. Note that fwrite have to write the output
as if it was written with fputc. That is, for each element to be
written (the third argument) fputc shall write out as many bytes
as specified by the second argument. Each of these bytes shall be
successive. (Of course, an implementation need only behave as-if
fputc is being used).

--
Thomas.


这篇关于一个简单的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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