学习C语言的第14天

字符串的输入——fgets()

从键盘输入字符,保存到所指定的内存空间,直到出现换行符、读到文件结尾或读到元素个数-1为止,最后会自动加上‘\0’作为字符串结束。

键盘输入的字符串,固定写为stdin。

fgets()在读取一个用户通过键盘输入的字符时,同时把用户输入的回车键也作为字符串的一部分。scanf和fgets输入一个字符串的时候,不包含结尾的“\n",**但通过fgets结尾多了”\n"。**fgets()函数是安全的,不存在换从区溢出的情况。

#include
int main()
{
	char ch[10];
	fgets(ch,sizeof(ch),stdin);
    \\fgets可以接收空格
	printf("%s",ch);
	return 0;
 } 
1输出结果:
    	hello
    	hello
    

此时存入的字符串为hello\n\0

2输出结果:
    	helloworld
    	helloworl

此时存入的字符串为helloworl\n

fgets获取字符串少于元素个数会有\n,大于等于没有\n

字符串的打印 ——puts()

标准设备输出字符串,在输出完成后自动输出一个\n.

#include
int main()
{
    1\\char ch[]="helloworld";
    \\puts(ch);
    \\自带换行
    puts("hello\0world");
     \\“hello\0world"占13个字符,\0占两个,结尾\01return 0;
}
1输出结果:helloworld
    	
输出结果:hello
    

fputs

将str(字符串)所指定的字符串写入到stream(文件指针)指定的文件中,字符串结束符‘\0不写如文件。

#include
int main()
{
 char ch[]="hello world";
    fputs(ch,stdout);
    return 0;
}
输出结果:hello world

字符串长度

strlen

计算指定字符串s的长度,不包含字符串结束字符’\0’

s 字符串首地址

#include
int main()
{
    char ch[100]="hello world";
    \\加上'\0'字符串长度为12
    printf("字符串大小:%d"sizeof(ch));
    printf("字符串长度:%d“,strlen(ch));
           return 0;
}
输出结果:字符串大小:100
    	字符串长度:11

你可能感兴趣的:(学习,c语言,开发语言)