putc需要stdout,而puts [英] putc needs stdout, vs puts

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

问题描述

C历史问题在这里.为什么C函数putc需要第二个参数,如

C history question here. Why does the C function putc require a second parameter like

putc( 'c', stdout ) ;

看跌期权更方便

puts( "a string" ) ;

msvc ++中有一个功能

There is a function in msvc++

putchar( 'c' ) ;

哪种工作方式可能会期望putc起作用.我以为putc的第二个参数是能够将putc定向到文件,但是为此有一个功能fputc.

Which works the way one might expect putc to work. I thought the second parameter of putc was to be able to direct putc to a file, but there is a function fputc for that.

推荐答案

int putc ( int character, FILE * stream );

向流中写入一个字符,并向前移动位置指示器.
因此,它是比putchar
更通用的函数 其他功能可以基于此,例如

Writes a character to the stream and advances the position indicator.
So it is a more generic function than putchar
Other functions can be based on this e.g.

#define putchar(c) putc((c),stdout)  

根据Kernighan的书putcfputc等效,但是putc可以实现为宏,而putc 可能不得不多次评估其流参数.
我已经读过,据说两者都存在是为了向后兼容,但是不确定这是否有效

According to Kernighan's book putc is equivalent with fputc but putc could be implemented as a macro and putc may have to evaluate its stream argument more than once.
I have read that supposedly that both exist for backward compatibility, but not sure if this is valid

这篇关于putc需要stdout,而puts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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