C语言 字符串数组

来源:百度知道 编辑:UC知道 时间:2024/05/22 03:57:35
什么叫空白字符?老谭的书中第四章%s是这样解释的:用来输入字符串,将字符串送到一个字符数组中,在输入时以非空白字开始,以空白字符结束.字符以’\0’作为最后一个字符.这句不懂:一空白字符结束了怎么还来个\0作为最后一个字符.?
#include <stdio.h>
main()
{
char c[]="pascal program";
scanf("%s",&c);
printf("%s",c);
}
这个程序要怎么输入”i am boy",每次都只输出一个i.

#include <stdio.h>
main()
{
char c[]="pascal program";
gets(c);
printf("%s",c);
}
这个可以的,如果你一定要用sanf输出i am a boy,这样吧:
#include<stdio.h>
main()
{char c1[10],c2[10],c3[10],c4[10];
scanf("%s%s%s%s",c1,c2,c3,c4);
printf("%s %s %s %s",c1,c2,c3,c4);
}
因为输入i am a boy,实际上是四个字符串。以空格为字符结束,系统自动加上'/0'进行存储。如果输入其他四个字符串的也可以。

scanf();是以空格,回车来做为结束的。
你这里不要用scanf();
你用gets(c);

#include <stdio.h>
main()
{
char c[]="pascal program";
gets(c);
printf("%s",c);
}

呵呵。。。。scanf只能显示一个字符。。。。。。
用循环语句。。。。。。

请仔细阅读潭浩强C语言第六章,scanf输入时遇到空格表示输入结束