两者之间到底有什么区别?
正如其他评论者所指出的(并且从文档中显而易见),printf
的最大区别在于它允许格式化参数。
也许您想知道当没有向 printf()
传递额外参数时,这两个函数是否等效?很明显,它们不等效。
char * str;
FILE * stream;
...
fputs(str,stream); // this is NOT the same as the following line
fprintf(stream,str); // this is probably wrong
第二种方法可能是错误的,因为传递给 fprintf()
的字符串参数仍然是一个格式化字符串:如果它包含 '%' 字符,则会被解释为格式化说明符。功能上等效(但不够直观、高效和美观)的形式是: fprintf(stream,"%s", str);
嗯...puts()
函数只是写入一个字符串,而printf()
函数则提供了多种格式化数据的工具。
fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
文档很有用!学会阅读它,你就会拥有一个强大的工具。
fputs(s, f)
的功能与fprintf(f, "%s", s)
等效。现代优化编译器将它们编译为相同的代码(调用fputs
)。 - chqrlie