çscanf函数与空间问题 [英] C scanf with spaces problem

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf\">How你是否允许使用scanf函数输入空格?

的printf(请书名\\ N键);
    scanf函数(%S,BOOKNAME);

printf("please key in book title\n"); scanf("%s",bookname);

我喜欢这里面的数据: -
C程序设计

i inside the data like this :- C Programming

但为什么输出这样的数据: -
ç

but why output the data like this :- C

失去编程(串)?

为什么

感谢。

推荐答案

%S 转换说明原因 scanf函数停在第一个空格字符。如果您需要能够读取空白字符,你要么需要使用%[转换说明,如

The %s conversion specifier causes scanf to stop at the first whitespace character. If you need to be able to read whitespace characters, you will either need to use the %[ conversion specifier, such as

 scanf("%[^\n]", bookname);

这将读到的一切到下一个换行符,并将其存储到 BOOKNAME ,但为了安全起见,你应该在指定的转换说明BOOKNAME的最大长度;例如如果BOOKNAME有房间30个字符计数空终止符,你应该写

which will read everything up to the next newline character and store it to bookname, although to be safe you should specify the maximum length of bookname in the conversion specifier; e.g. if bookname has room for 30 characters counting the null terminator, you should write

 scanf("%29[^\n]", bookname);

另外,你可以使用与fgets()

 fgets(bookname, sizeof bookname, stdin);

我preFER的与fgets()解决方案,个人。

这篇关于çscanf函数与空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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