c语言puts函数用法是什么?
c语言puts函数用法是:puts();
puts()函数用来向标准输出设备(屏幕)输出字符串并换行,具体为:把字符串输出到标准输出设备,将''转换为回车换行。其调用方式为,puts(s);其中s为字符串字符(字符串数组名或字符串指针)。
功能:将字符串输出到终端,puts函数一次只能输出一个字符串,字符串中可以包括转义字符。
函数原型:int puts(const char *string);
复制代码1
2
3
4
5
6
7
#include <stdio.h>
int main(void)
{
char string[] = "This is an example output stringn";
puts(string);
return 0;
}
登录后复制
登录后复制
初学者要注意以下例子
复制代码1
2
3
4
5
6
7
#include <stdio.h>
int main(void)
{
char string[] = "This is an example output stringn";
puts(string);
return 0;
}
登录后复制
登录后复制
从此例中可看到puts输出字符串时要遇到'’也就是字符结束符才停止,如上面的程序加上一句 string[10]='';
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <conio.h>
int main(void)
{
int i;
char string[20];
for(i=0;i<10;i++)
string[i]='a';
string[10]='';
puts(string);
getch();
return 0;
}
登录后复制
运行就正确了
*注:
(1) puts()函数只能输出字符串, 不能输出数值或进行格式变换。
(2)可以将字符串直接写入puts()函数中。如:
puts("Hello, world!");
(3) puts 和 printf的用法一样,puts()函数的作用与语句“printf("%sn",s);的作用相同。注意:puts在输出字 符串后会自动输出一个回车符。
puts()函数的一种实现方案如下:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int puts(const char * string)
{
const char * t = string;
const char * v = string;
int i = 0;
while(*t!='')
{
i++;
t++;
}
int j = 0;
for(j;j<=i;j++)
putchar((v[j]));
putchar('n');
return 0;
}
登录后复制
推荐教程:《C语言》
以上就是c语言puts函数用法是什么?的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是痴情魔镜最近收集整理的关于c语言puts函数用法是什么?的全部内容,更多相关c语言puts函数用法是什么内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复