输入输出都不会的小白,请指教。

xutongpass 2016-02-18 10:03:33
#include <stdio.h>

int main(void)
{
char a1,b1;
char c1;
printf("input the char\n");
scanf("%c%c%c",&a1,&b1,&c1);

printf("%c\n%c\n%c\n",a1,b1,c1);

return 0;
}
这个代码运行以后,输入 q w e,然后回车,输出是q(换行)(空行)e,
请问这是啥情况。
...全文
166 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxq1994929 2016-02-20
  • 打赏
  • 举报
回复
格式规范的领域:scanf和wscanf功能 格式规范具有以下形式: %
  • [宽度] [ { H | L | I64 | L } ]型 格式参数指定输入的解释,并且可以包含以下的一个或多个: 空格字符:空白('');标签('\t');或换行符('\n')。一个空格字符导致scanf读取,但没有商店,所有连续的空格字符输入到下一个非空格字符–。格式中的一个空格字符匹配任意数量(包括0)和在输入中的空格字符的组合。 非空白字符,除了%符号(%)。一个非空格字符的原因–scanf读取,但不存储,–匹配非空白字符。如果在输入下一个字符不匹配,scanf终止。 格式规范,介绍了百分之百的符号(%)。一个格式说明原因scanf读取和转换字符在输入到一个指定类型的值。该值被分配给参数列表中的一个参数。 格式是从左到右读。在格式规范有望匹配输入字符序列特征;匹配字符在输入扫描但不存储。如果一个字符在输入与格式规范,冲突scanf终止,和字符留在stdin为未读。 当遇到第一种格式规范时,根据本规范将第一输入字段的值转换,并存储在第一个参数指定的位置中。二次格式规范使得二次输入字段被转换并存储在第二个参数中,因此通过格式字符串的结尾。 输入字段定义为所有字符到第一个空白字符(空格,制表符,换行符,或或),不能按规范格式转换的第一个字符,或直到字段宽度(如果指定)达到。如果有太多的参数,对于给定的规格,额外的参数进行评估,但忽略。如果格式规范没有足够的参数,结果是不可预测的。 的格式规范的各个领域是一个字符或一个数字代表一个特定格式选项。的类型字符,它出现在上一个可选的格式字段,确定输入字段是否被解释为一个字符,一个字符串,或一个数字。 最简单的格式规范仅包含%符号和类型字符(例如%)。如果百分之一个符号(%)后跟一个没有意义的字符,字符和下面的字符(最多为下一个符号)被视为一个普通的字符序列,即必须匹配输入的字符序列。例如,要指定百分之一个符号字符为输入,请使用%。 星号(*)以下百分号抑制下一个输入字段赋值,它被解释为一个字段指定类型的。该字段被扫描但未存储。
xutongpass 2016-02-18
  • 打赏
  • 举报
回复
多谢各位提醒,最起码的用法都不知道。
paschen 2016-02-18
  • 打赏
  • 举报
回复
scanf那句改成:scanf("%c %c %c",&a1,&b1,&c1); 然后输入q w e 中间用一个空格分割
王狗狗 2016-02-18
  • 打赏
  • 举报
回复
galiniur0u 2016-02-18
  • 打赏
  • 举报
回复
楼主确定没说错么,不是 q(换行)(空行)w么?因为scanf的输入模式和双引号""中的必须一样。 也就是"%c%c%c",输入的时候必须连着,qwe,如果按照楼主这么输入的话就应该是"%c %c %c"。中间加空格了。
赵4老师 2016-02-18
  • 打赏
  • 举报
回复
Format Specification Fields: scanf and wscanf Functions A format specification has the following form: %
  • [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧