puts() 原型
int puts(const char *str);
puts()
函数以一个以 null 结尾的字符串 `str` 作为参数,并将其写入 `stdout`。终止的 null 字符 '\0' 不会被写入,但它会在写入字符串后添加一个换行符 '\n'。
fputs()
和 puts()
的主要区别在于 puts()
函数会在输出末尾添加一个换行符,而 fputs()
函数则不会。
它定义在 <cstdio> 头文件中。
puts() 参数
str
:要写入的字符串。
puts() 返回值
成功时,puts()
函数返回一个非负整数。失败时,它返回 EOF
并设置 stdout
上的错误指示符。
示例:puts() 函数的工作原理
#include <cstdio>
int main()
{
char str1[] = "Happy New Year";
char str2[] = "Happy Birthday";
puts(str1);
/* Printed on new line since '/n' is added */
puts(str2);
return 0;
}
运行程序后,输出将是
Happy New Year Happy Birthday
另请阅读