scanf(),printf()和gets(),puts()在输入输出字符串时的区别

1.scanf(“%s”,str)和gets(str)

scanf(“%s”,str)和gets(str)均可用于输入字符串到字符数组变量str,但scanf(“%s”,str)匹配连续的一串非空白字符,香港虚拟主机,遇到空格、tab或回车即结束,字符串前的空白字符没有存入str,香港服务器租用,只表示输入还未开始(感谢garbageMan的指正),而gets(str)读到回车处结束,所以当句子中单词由空格分开时要用后者来输入,如下图所示:

需要强调一点,香港服务器,scanf(“%s”,str)在遇到’\n’(回车)或’ ‘(空格)时输入结束,但’\n’(回车)或’ ‘(空格)停留在出入缓冲区,如处理不慎会影响下面的输入;gets(str)遇到’\n’(回车)时输入结束,但’\n’(回车)已被替换为’\0’,存储于字符串中,输入缓冲中没有遗留的’\n’(回车),不会影响后续的输入。测试程序的代码为:

View Code

#include<iostream>#include<stdio.h>using namespace std;int main(){ str[80]; char ch; cout<<<<endl; scanf(,str); cout<<<<str<<endl; cout<<<<endl; &&ch!=EOF); gets(str); cout<<<<str<<endl; cout<<<<endl; scanf(,str); cout<<<<str<<endl; cout<<<<endl; &&ch!=EOF); gets(str); cout<<<<str<<endl; return 0;}我们什么都没有,唯一的本钱就是青春。

scanf(),printf()和gets(),puts()在输入输出字符串时的区别

相关文章:

你感兴趣的文章:

标签云: