gets()和scanf()混用问题

2020-09-28 体育 122阅读

scanf(..);输入字符按下回车键后,gets()函数会把回车键当做输入的字符。所以在执行gets()前要把回车过滤掉!!


改正后代码:

#include

main()

{

  char a1[5] = "",a2[5] = "",a3[5] = "",a4[5] = "";

scanf("%s%s",a1,a2);

printf("a1=%s\n",a1);

printf("a2=%s\n",a2);

printf("a3=%s\n",a3);

printf("a4=%s\n",a4);

printf("请输入a3:");

getchar();    //过滤回车

gets(a3);printf("a1=%s\n",a1);

printf("a2=%s\n",a2);

printf("a3=%s\n",a3);

printf("a4=%s\n",a4);


printf("请输入a4:");

gets(a4);

printf("a1=%s\n",a1);

printf("a2=%s\n",a2);

printf("a3=%s\n",a3);

printf("a4=%s\n",a4);

}


声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com