Scanf 不取整串 [英] Scanf not taking whole string

查看:35
本文介绍了Scanf 不取整串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main (){
    char test1 [25];
    printf("Enter a string:\n");
    scanf("%s",&test1);
    printf("%s\n",test1);
}

此代码应简单地打印并输入字符串.如果我输入Hello Wolrd",结果只会打印Hello".我该如何解决这个问题?

This code should simply print and entered string. If I enter "Hello Wolrd" it only prints "Hello" as a result. How can I fix this?

推荐答案

scanf with %s 只读取直到空格或换行.而是使用 fgets.

scanf with %s reads only until space or new line. Instead use fgets.

  char buffer[256];
  fgets(buffer, 256, stdin);

这篇关于Scanf 不取整串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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