C语言里的puts()函数怎么用

2025-02-27 21:52:12
推荐回答(5个)
回答1:

puts就是输出字符串啊。

int puts(
const char* string
);

MSDN的例子
/* PUTS.C: This program uses puts
* to write a string to stdout.
*/

#include

void main( void )
{
puts( "Hello world from puts!" );
}

运行结果就是
Hello world from puts!

你要输出换行的话,就用 puts( "\n" );

用法很简单啊,就是把一个C样式的字符串当参数传过去。

//-----------------------------------------

我刚刚试过了
puts( "" )的确可以起到换行的作用。

The puts function writes string to the standard output stream stdout, replacing the string's terminating null character ('\0') with a newline character ('\n') in the output stream.

当puts遇到\0时,会输出一个\n,也就是换行。
所以puts( "" )时,因为字符串本身长度为0,所以第一个字符就是\0,puts会输出一个\n,所以起到了换行的效果。

也就是说, puts( "" )跟puts( "\0" )是等效的,也等效於printf( "\n" )

回答2:

puts是输出函数。
只有一个参数 要求是字符串。
比如
puts("abc");
函数功能 是输出这个字符串,并加上换行
相当于printf("abc\n");

也就是说 puts是printf的简化版,更高效一些。

回答3:

puts是输出一个字符串到控制台,并自动换行。
所以你的put(“”)就能起到换行功能!

回答4:

puts()函数是C语言中的输出函数。
uts()函数用来向标准输出设备(屏幕)写字符串并换行,其调用方式为,puts(s);其中s为字符串字符(字符串数组名或字符串指针)。
函数原型:int puts(const char *string);
参数:string const的字符类型的指针
返回值: int类型,执行成功输出的字节数,执行失败返回EOF。
注意:puts输出字符串时要遇到'\0’也就是字符结束符才停止,所以在字符串的最后一个要是 '\0'符。
实例:
#include
#include
int main(void)
{
int i;
char string[20];
for(i=0;i<10;i++)
string[i]='a';
string[10]='\0';//注意
puts(string);
getch();
return 0;
}

回答5:

o