stdlib函数中的参数顺序不一致 [英] Inconsistent argument order in stdlib functions

查看:60
本文介绍了stdlib函数中的参数顺序不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难保持这些东西的直接......

例如,fputs就像fprintf一样putf到printf,除了

fputs最后有文件句柄,开头有fprintf!

非常不合逻辑!很难记住。


现在这可以通过宏很容易地纠正,例如:


#include< stdio.h>


#define fputs(X,Y)fputs(Y,X)


main()/ *注意:返回int not void! ! * /

{

fprintf((FILE *)stdout,a test\ n);

fputs((文件*) )stderr,Another test\\\
); / *一致性! * /

返回0;

}


所以基本上我打算建立一个宏定义列表给

调节我在stdlib中遇到的任何不一致,然后我可以将它们放在标题中,以便在我的所有程序中都包含#included。但是肯定

其他人已经这样做了,所以我想我会问一下在重新发明轮子之前是否有任何这样的清单可用。

解决方案

Fr ** **********@googlemail.com 写道:


我发现很难保留其中的一些东西直接...

例如,fputs就像fprintf一样put到printf,除了

fputs最后有文件句柄,开头有fprintf! br />
非常不合逻辑!而且很难记住。



一个好的经验法则是FILE指针是最后的,除了

用于具有可变数量参数的函数,在这种情况下

它首先出现。


现在这可以通过宏很容易地纠正,例如:


#include< stdio.h>


#define fputs(X,Y)fputs(Y,X)



我建议这样做。首先,允许实现

从每个标准库中声明一个宏

函数。另一方面,你会混淆任何读取代码的C程序员。

-

有些人*是*傲慢的,和其他人阅读常见问题解答。

--Chris Dollin


Fr ************ @ googlemail.com 写于03/09/07 13:24,:
< blockquote class =post_quotes>
我发现很难保持这些东西的直接......

例如,fputs就像fprintf一样put到printf,除了

fputs在结尾处有文件句柄而在开头有fprintf!

非常不合逻辑!而且很难记住。



是的,这是不一致的,但通过练习你会发现

不难记住。不管你经常使用的函数

,无论如何:我承认,尽管有三十多年的时间,但在b中用b b编程的b $(b)的参数顺序

仍然逃避我(我几乎没有使用过它),而且我仍然对于fread()和

fwrite()的中间两个参数感到困惑。 。但是那从来没有让我感到困扰,因为在

我想要使用其中一种功能的时候我会用任何方便的参考来查看它们:man页面,

信息屏幕,甚至(喘气!)一本印在纸上的书。这不是很难。


要了解不一致的原因,你需要确认C库中的
从Jove的额头开始,没有全力武装。它不是由一个人发明的,在一个地方,甚至是一次,但是在很多地方经过一段时间的演变,随着人们借鉴他们的想法'd

在其他地方看到并改善在各种各样的(有时

不兼容)方式。今天的图书馆是一个

融合了多个不同图书馆的各种风格

和各种严谨程度。


进化就是这样 - 或者你最近没想过

你的蠕虫阑尾?


现在这很容易纠正通过宏,例如:


#include< stdio.h>


#define fputs(X,Y)fputs(Y, X)



你需要从`#undef fputs'开始,以防止

< stdio.h已定义的可能性`fputs''

作为一个宏。


哦,亲爱的主啊,亲爱的耶稣,不要这样做,我求求!

如果你只是*必须*介绍宏来安排你喜欢的东西,至少给他们一些看起来不像的名字

其他的东西!你的代码将是完全不可读的,并且(你会发现)不可读的代码往往会获得异常高密度的错误 - 然后转向

不可维护。当你编写代码时,你将用私人语言编写

,其他人也不会理解 - 而你

也会找到你自己阅读共同语言的能力

减少了。如果你不能没有你的傻宏,

至少有礼貌称它为'BFTSPLX''。


main()/ *注意:返回int not void !! * /



(但不能长久! - Sweeney Todd)新的

" C99"标准的版本,省略

类型是违法的。键入eye enn tee space保存击键,将

与你必须输入的评论进行比较以防止遗漏。


{

fprintf ((FILE *)stdout,A test\ n);



无用演员,因为`stdout''已经是'FILE *''。或者,

换句话说,为什么你不写呢


fprintf((FILE *)stdout,(char *)" A test \\\
");


fputs((FILE *)stderr,Another test\\\
); / *一致性! * /



再次,`stderr''已经是'FILE *''。


return 0;

}


所以基本上我打算建立一个宏定义列表来支持
来调节我来的任何不一致通过在stdlib中,然后我可以将它们放在标题中,以便在我的所有程序中都包含#included。但是肯定

其他人已经完成了这个,所以我想我会问,在重新发明轮子之前,是否有任何这样的列表可用。



我确信没有人在他的正确思想中做过这样的事情。

如果必须的话,发明你的车轮;我相信它会让你滚动

直接进入Perdition。


愚蠢的一致性是小脑袋的大人物,

受到小政治家,哲学家和神职人员的崇拜。

- Ralph Waldo Emerson


那条腿足够长;拉上另一个。

- 匿名


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


Ben Pfaff写道:

Fr ************ @ googlemail。 com 写道:



< snip>


>现在这可以通过宏很容易地纠正,例如:

#include< ; stdio.h>

#define fputs(X,Y)fputs(Y,X)



我建议这样做。首先,允许实现

从每个标准库中声明一个宏

函数。另一方面,你会混淆任何读取代码的C程序员。



这是*不会*?


-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......它仍然有效......)


I''m finding it really hard to keep some of these things straight...
For example, fputs is to puts as fprintf is to printf, except that
fputs has the file handle at the end and fprintf at the beginning!
Very illogical! And hard to remember.

Now this can quite easily be corrected by macros, for example:

#include <stdio.h>

#define fputs(X,Y) fputs(Y,X)

main() /* note: returning int not void!! */
{
fprintf((FILE *) stdout, "A test\n");
fputs((FILE *) stderr, "Another test\n"); /* consistency reigns! */
return 0;
}

So basically I was planning to build up a list of macro definitions to
regulate whatever inconsistencies I come through in stdlib, then I can
put them in a header to be #included in all my programs. But surely
other people will already have done this, so I thought I''d ask if any
such list is available somewhere before reinventing the wheel.

解决方案

Fr************@googlemail.com writes:

I''m finding it really hard to keep some of these things straight...
For example, fputs is to puts as fprintf is to printf, except that
fputs has the file handle at the end and fprintf at the beginning!
Very illogical! And hard to remember.

A good rule of thumb is that the FILE pointer comes last, except
for functions with a variable number of arguments, in which case
it comes first.

Now this can quite easily be corrected by macros, for example:

#include <stdio.h>

#define fputs(X,Y) fputs(Y,X)

I would recommend doing that. For one thing, the implementation
is allowed to declare a macro from each standard library
function. For another, you''ll confuse any C programmer who reads
your code.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin


Fr************@googlemail.com wrote On 03/09/07 13:24,:

I''m finding it really hard to keep some of these things straight...
For example, fputs is to puts as fprintf is to printf, except that
fputs has the file handle at the end and fprintf at the beginning!
Very illogical! And hard to remember.

Yes, it''s inconsistent, but with practice you''ll find
it is not hard to remember. Not for the functions you use
frequently, anyhow: I confess that despite thirty-plus years
of programming in C the order of the arguments to bsearch()
still eludes me (I scarcely ever use it), and I still get
confused about the middle two arguments to fread() and
fwrite(). But that''s never bothered me much, because on
the occasions when I want to use one of these functions I
just look them up in any convenient reference: man page,
info screen, or even (gasp!) a book printed on paper. It''s
not difficult.

To understand the reason for the inconsistencies, you
must realize that the C library did not spring full-armed
from the brow of Jove. It was not invented by one person,
at one place, or even at one time, but evolved over a span
of years at many places, with people borrowing ideas they''d
seen elsewhere and "improving" on them in assorted (sometimes
incompatible) ways. The library as it stands today is a
fusion of multiple different libraries with various styles
and various levels of rigor.

Evolution is like that -- or haven''t you thought about
your vermiform appendix lately?

Now this can quite easily be corrected by macros, for example:

#include <stdio.h>

#define fputs(X,Y) fputs(Y,X)

You need to start with `#undef fputs'' to guard against
the possibility that <stdio.hhas already defined `fputs''
as a macro.

But oh, dear Lord, sweet Jesus, don''t do this, I beg!
If you simply *must* introduce macros to arrange things to
your liking, at least give them names that don''t look like
something else! Your code will be completely unreadable,
and (you will discover) unreadable code tends to acquire
an unusually high density of bugs -- and then turn out to
be unmaintainable. When you write code you will be writing
in a private language nobody else understands -- and you
will also find your own ability to read the common language
diminished. If you cannot live without your silly macro,
at least have the decency to call it `BFTSPLX''.

main() /* note: returning int not void!! */

("But not for long!" -- Sweeney Todd) Under the new
"C99" version of the Standard, it is illegal to omit the
type. Typing "eye enn tee space" saves keystrokes, compared
to the comment you had to type to defend its omission.

{
fprintf((FILE *) stdout, "A test\n");

Useless cast, since `stdout'' is already a `FILE*''. Or,
to put it another way, why didn''t you write

fprintf((FILE *)stdout, (char*)"A test\n");

fputs((FILE *) stderr, "Another test\n"); /* consistency reigns! */

Again, `stderr'' is already a `FILE*''.

return 0;
}

So basically I was planning to build up a list of macro definitions to
regulate whatever inconsistencies I come through in stdlib, then I can
put them in a header to be #included in all my programs. But surely
other people will already have done this, so I thought I''d ask if any
such list is available somewhere before reinventing the wheel.

Nobody in his right mind has done such a thing, I am sure.
Invent your wheel if you must; I am sure it will roll you
straight to Perdition.

"A foolish consistency is the hobgoblin of little minds,
Adored by little statesmen and philosophers and divines."
-- Ralph Waldo Emerson

"That leg''s long enough; pull on the other."
-- Anonymous

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


Ben Pfaff wrote:

Fr************@googlemail.com writes:

<snip>

>Now this can quite easily be corrected by macros, for example:

#include <stdio.h>

#define fputs(X,Y) fputs(Y,X)


I would recommend doing that. For one thing, the implementation
is allowed to declare a macro from each standard library
function. For another, you''ll confuse any C programmer who reads
your code.

Is that *wouldn''t*?

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


这篇关于stdlib函数中的参数顺序不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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