scanf函数读" Enter]键。键 [英] scanf reading "Enter" key

查看:139
本文介绍了scanf函数读" Enter]键。键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我键入code下面的Enter键scanf函数不起作用?

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,字符** argv的)
{
 字符*味精=的malloc(100 * sizeof的(炭));
 做{
        scanf函数(%S,味精);
        的printf(%S \\ n,味精);
 }而(STRCMP(MSG,)!= 0);
}


解决方案
因为scanf函数的

()等字符字符串,用空格隔开,进入等,所以,它根本不理会进入,并等待真正的非空串。如果你想获得空字符串也是如此,
你需要使用

 与fgets(味精,100,标准输入);

Why scanf doesn't work when I type "Enter" in the code below?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char**argv)
{
 char *msg = malloc(100*sizeof(char));
 do{
        scanf("%s",msg);
        printf("%s\n",msg);
 } while(strcmp(msg,"")!=0);
}

解决方案

Because of scanf() wait char-string, separated by whitespaces, enters, etc. So, it just ignores ENTERs, and waiting for "real non-empty string". If you want to get empty string too, you need to use

fgets(msg, 100, stdin);

这篇关于scanf函数读&QUOT; Enter]键。键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆