为什么使用fprintf / size_t而不是printf / int [英] why use fprintf / size_t instead of printf/ int

查看:78
本文介绍了为什么使用fprintf / size_t而不是printf / int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



E. Robert Tisdale写道:


E. Robert Tisdale wrote:


int main(int argc,char * argv []){
quad_t m = { 0,1,2,3};
int r;
fprintf(stdout," m =(");
for(size_t j = 0; j< 4; + + j)


为什么你将j声明为size_t类型?

fprintf(stdout,"%d",m [j]);
fprintf(stdout,")\ n");


你在这里使用fprintf是什么意思?

w0(& r,m);
fprintf(stdout," r =%d \ n",r);
返回0;

int main(int argc, char* argv[]) {
quad_t m = {0, 1, 2, 3};
int r;
fprintf(stdout, "m = (");
for (size_t j = 0; j < 4; ++j)
Why did you declare j as type size_t ?
fprintf(stdout, " %d", m[j]);
fprintf(stdout, ")\n");
WHy did you use fprintf here?
w0(&r, m);
fprintf(stdout, "r = %d\n", r);
return 0;




我只是在问,因为我是那些开始使用的人之一

函数如gets,strcat,atol和strcpy,然后被建议

使用替代品(fgets,strncat,strtol和strncpy)。也许你打电话给

告诉我什么类型的潜力你试图通过使用

fprintf和size_t来防止的错误。


谢谢


Gaya



I''m only asking because I''m one of those people who started out using
functions like gets, strcat, atol and strcpy before being adviced to
use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call
tell me what type of "potential" bug you are trying to prevent by using
fprintf and size_t.

Thanks

Gaya

推荐答案

G Patel写道:
G Patel wrote:
E. Robert Tisdale写道:
E. Robert Tisdale wrote:
int main(int argc,char * argv []){
quad_t m = {0,1,2,3};
int r;
fprintf(stdout, " m =(");
for(size_t j = 0; j< 4; ++ j)
int main(int argc, char* argv[]) {
quad_t m = {0, 1, 2, 3};
int r;
fprintf(stdout, "m = (");
for (size_t j = 0; j < 4; ++j)



为什么要将j声明为size_t类型?



Why did you declare j as type size_t?

fprintf(stdout,"%d",m [j]);
fprintf(stdout,")\ n");
fprintf(stdout, " %d", m[j]);
fprintf(stdout, ")\n");

<你在这里使用fprintf是什么意思?



WHy did you use fprintf here?

w0(& r,m);
fprintf(stdout," r =%d \\ \\ n",r);
返回0;
w0(&r, m);
fprintf(stdout, "r = %d\n", r);
return 0;



我只是在问,因为
我是其中之一软管开始使用函数的人如获取​​,strcat,atol和strcpy,然后才被建议
使用替代品(fgets,strncat,strtol和strncpy)。
也许你打电话告诉我什么潜在的类型错误
你试图通过使用fprintf和size_t来阻止。



I''m only asking because
I''m one of those people who started out using functions
like gets, strcat, atol and strcpy before being advized
to use alternatives (fgets, strncat, strtol, and strncpy).
Maybe you call tell me what type of "potential" bug
you are trying to prevent by using fprintf and size_t.




不要在这里寻找深刻的推理。

这主要是风格问题。

我使用size_t作为下标j和范围n因为


0< = j< n

数组声明中


int m [n];


我应该写了类似的东西:


FILE * myout = stdout;

fprintf(myout," m =(");

for(size_t j = 0; j< 4; ++ j)

fprintf(myout,"%d",m [j]);

fprintf(myout,")\ n");


但我很懒,

我不想分散对问题的注意力在手边。

但是,如果稍后我决定

我真的需要将此输出重定向到日志文件,这很方便。



Don''t look for profound reasoning here.
It''s mostly a matter of style.
I use size_t for subscript j and extent n because

0<= j < n

in the declaration of array

int m[n];

I should have written something like:

FILE* myout = stdout;
fprintf(myout, "m = (");
for (size_t j = 0; j < 4; ++j)
fprintf(myout, " %d", m[j]);
fprintf(myout, ")\n");

but I was lazy and
I didn''t want to distract attention from the problem at hand.
But this is convenient if, later, I decide that
I really need to redirect this output to a log file for example.


2005年1月25日19:10:03 -0800,G Patel < GA ******** @ gmail.com>在comp.lang.c中写了


On 25 Jan 2005 19:10:03 -0800, "G Patel" <ga********@gmail.com> wrote
in comp.lang.c:

E. Robert Tisdale写道:

E. Robert Tisdale wrote:

int main(int argc,char * argv []){
quad_t m = {0,1,2,3};
int r;
fprintf(stdout," m =( ");
for(size_t j = 0; j< 4; ++ j)
为什么要将j声明为size_t类型?

int main(int argc, char* argv[]) {
quad_t m = {0, 1, 2, 3};
int r;
fprintf(stdout, "m = (");
for (size_t j = 0; j < 4; ++j)
Why did you declare j as type size_t ?




Tisdale是一个巨魔和白痴。在这种情况下,绝对没有理由将size_t更改为int。



Tisdale is a troll and an idiot. There is absolutely no reason at all
to prefer size_t to int in this situation.

fprintf(stdout ,%d,m [j]);
fprintf(stdout,")\ n");
fprintf(stdout, " %d", m[j]);
fprintf(stdout, ")\n");



你为什么在这里使用fprintf?



WHy did you use fprintf here?




Tisdale仍然是一个巨魔和白痴。绝对没有理由

更喜欢fprintf(stdout,/ *无论* /)到printf(/ *无论* /)

这里,或在任何情况下我可以想到副手。

虽然如果有不同的情况,有人在这里

会纠正我。


有时候使用

fputs(text_string,stdout)而不是puts(text_string),当你不这样做时,

由于某种原因,想要一个''\ n''附加,否则你没有使用

printf()。



Tisdale is still a troll and an idiot. There is absolutely no reason
to prefer fprintf(stdout, /* whatever */) to printf(/* whatever */)
here, or under any circumstances that I can think of off-hand.
Although if there is a case where there is a difference, someone here
will correct me.

There are times when it can be considered reasonable to use
fputs(text_string, stdout) over puts(text_string), when you do not,
for some reason, want a ''\n'' appended and you are otherwise not using
printf().

w0 (& r,m);
fprintf(stdout," r =%d \ n",r);
返回0;
w0(&r, m);
fprintf(stdout, "r = %d\n", r);
return 0;



我我只是问,因为我是那些开始使用
函数的人之一,比如获取,strcat,atol和strcpy,然后才被建议使用替代品(fgets,strncat,strtol和strncpy) )。也许你打电话
告诉我什么类型的潜力你试图通过使用
fprintf和size_t来防止的错误。

谢谢



I''m only asking because I''m one of those people who started out using
functions like gets, strcat, atol and strcpy before being adviced to
use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call
tell me what type of "potential" bug you are trying to prevent by using
fprintf and size_t.

Thanks




提出问题没有错,如果你不知道海报的相对可靠性

。你可以从这个特定的交易所收集的最好的建议是

建议是忽略

Tisdale。期间。


就个人而言,我有他杀人档案,只看到他的帖子为

就像你在回复中引用的那样。
< br $> b $ b -

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http: //www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a .. .FAQ-acllc.html



There is nothing wrong with asking the question, if you don''t know the
relative reliability of the posters. The best piece of advice you
advice you can gather from this particular exchange is to ignore
Tisdale. Period.

Personally, I have him kill filed, and only see such of his posts as
people like you quote in replies.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


G Patel写道:
G Patel wrote:

E. Robert Tisdale写道:

E. Robert Tisdale wrote:

int main(int argc,char * argv []){
quad_t m = {0,1,2,3};
int r;
fprintf(stdout," m =(");
for(size_t j = 0; j< 4; ++ j)

int main(int argc, char* argv[]) {
quad_t m = {0, 1, 2, 3};
int r;
fprintf(stdout, "m = (");
for (size_t j = 0; j < 4; ++j)



为什么将j声明为size_t类型?



Why did you declare j as type size_t ?

fprintf(stdout,"%d",m [j ]);
fprintf(stdout,")\ n");
fprintf(stdout, " %d", m[j]);
fprintf(stdout, ")\n");



你为什么在这里使用fprintf?



WHy did you use fprintf here?

w0(& r,m);
fprintf(stdout," r =%d \ n",r);
返回0;
w0(&r, m);
fprintf(stdout, "r = %d\n", r);
return 0;


我只是问,因为我是那些开始使用
函数的人之一,比如获取,strcat,atol和strcpy,然后才被建议使用替代品(fgets,strncat,strtol,和strncpy)。也许你打电话
告诉我什么类型的潜力你试图通过使用
fprintf和size_t来防止的错误。



I''m only asking because I''m one of those people who started out using
functions like gets, strcat, atol and strcpy before being adviced to
use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call
tell me what type of "potential" bug you are trying to prevent by using
fprintf and size_t.




Tisdale有很多奇怪的编码习惯。以上不是错误的b $ b,但绝对不是优选的标准C编码

做法。


我的建议是你忽略他。在他最好的情况下,他只是个傻瓜,

但对他而言并不罕见。


Erik

-

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

Erik de Castro Lopo 没有**** @ mega-nerd.com (是的,这是有效的)

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

真正善于使用C ++就像是非常擅长使用岩石来获得b $ b锐化棒。 - Thant Tessman



Tisdale has a lot of strange coding practices. The above is not
wrong, but is definitely not preferable standard C coding
practices.

My suggestion is that you ignore him. At his best he''s just a fool,
but its not uncommon for him to be plain wrong.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it''s valid)
+-----------------------------------------------------------+
Being really good at C++ is like being really good at using rocks to
sharpen sticks." -- Thant Tessman


这篇关于为什么使用fprintf / size_t而不是printf / int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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