简体   繁体   English

为什么 C puts 追加换行符而 fputs 没有?

[英]Why does C puts appends a newline while fputs doesn't?

The C standard provides two functions, puts and fputs , with puts(s) behaving as fputs(s, stdout) except that it additionally appends a newline: C 标准提供了两个函数putsfputs ,其中puts(s)行为与fputs(s, stdout)相同,只是它另外附加了一个换行符:

The puts() function shall write the string pointed to by s , followed by a <newline> , to the standard output stream stdout . puts() function 应将s指向的字符串,后跟<newline>写入标准 output stream stdout

What is the rationale for this difference in behavior between puts and fputs ? putsfputs之间的这种行为差异的基本原理是什么?

The puts function specifically writes to stdout which is commonly a console. puts function 专门写入通常是控制台的stdout Because console output is typically line bufered, it's convenient to not have to explicitly add a newline to a string to print.因为控制台 output 通常是行缓冲的,所以不必在要打印的字符串中显式添加换行符很方便。

The fputs function can write to any given FILE object, not just stdout , so by not automatically adding a newline it makes the function more flexible in the general case. fputs function 可以写入任何给定的FILE object,而不仅仅是stdout ,因此通过不自动添加换行符,它使 function 在一般情况下更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM