user-0 提问: 为什么输入n之后直接输出了一大堆乱的数字

回复:
user: user-1
content: 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.  格式规范字段：scanf和wscanf函数 格式规范的格式如下： %[*][width][{h | l | I64 | l}]类型 format参数指定输入的解释，可以包含以下一个或多个： 空白字符：空白（“”）；制表符（'\t'）；或换行符（'\n'）。空白字符导致scanf读取（但不存储）输入中直到下一个非空白字符的所有连续空白字符。格式中的一个空白字符与输入中的任意数字（包括0）和空格字符的组合相匹配。 非空白字符，百分比符号（%）除外。非空白字符导致scanf读取但不存储匹配的非空白字符。如果stdin中的下一个字符不匹配，scanf终止。 格式规范，由百分号（%）引入。格式规范导致scanf读取输入中的字符并将其转换为指定类型的值。该值被分配给参数列表中的参数。 格式从左到右读取。格式规范之外的字符应与stdin中的字符序列相匹配；stdin中的匹配字符将被扫描但不会存储。如果stdin中的某个字符与格式规范冲突，scanf将终止，并且该字符将保留在stdin中，就像它没有被读取一样。 当遇到第一个格式规范时，第一个输入字段的值将根据该规范进行转换，并存储在第一个参数指定的位置。第二个格式规范将转换第二个输入字段并将其存储在第二个参数中，依此类推，直至格式字符串的末尾。 输入字段定义为所有字符，直到第一个空白字符（空格、制表符或换行符），或直到第一个字符无法根据格式规范转换，或直到达到字段宽度（如果指定）。如果给定规范的参数太多，将计算额外的参数，但会忽略这些参数。如果格式规范没有足够的参数，那么结果是不可预测的。 格式规范的每个字段都是表示特定格式选项的单个字符或数字。出现在最后一个可选格式字段之后的类型字符决定输入字段是被解释为字符、字符串还是数字。 最简单的格式规范只包含百分号和类型字符（例如%s）。如果百分号（%）后跟一个没有意义的字符作为格式控制字符，则该字符和后面的字符（直到下一个百分号）被视为普通字符序列，即必须与输入匹配的字符序列。例如，要指定要输入的百分号字符，请使用%%。 在百分号后面的星号（*）将禁止指定下一个输入字段，该字段被解释为指定类型的字段。字段已扫描但未存储。

user: user-2
content: 仅供参考： #include<stdio.h>  int main() {     int t;     scanf("%d",&t);     for(int i=0; i<t; i++)     {         int n;         char s[100];         scanf("%s",s);         scanf("%d",&n);         for(int p=0; p<n; p++)         {             int a,b,c; 	    //scanf("[%d:%d:%d]",&a,&b,&c); 	    char buf[1024]; 	    scanf("%s", buf); 	    sscanf(buf, "[%d:%d:%d]",&a,&b,&c);             printf("%d %d %d",a,b,c);         }     }     return 0; }

user: user-3
content: 引用 7 楼 Aa_1z 的回复:[quote=引用 6 楼 5250的回复:]   scanf("[%d:%d:%d]",&a,&b,&c);改一下 scanf("%d:%d:%d",&a,&b,&c); 可是我想写成[1:3:4]这种形式，不知道怎么改[/quote]  这样改吧  将[]当作一个字符就行了     int a, b, c;     char d,e;     scanf("%c%d:%d:%d%c", &d,&a, &b, &c,&e);

user: user-0
content: 引用 6 楼 5250的回复:scanf("[%d:%d:%d]",&a,&b,&c);改一下 scanf("%d:%d:%d",&a,&b,&c); 可是我想写成[1:3:4]这种形式，不知道怎么改

user: user-3
content: scanf("[%d:%d:%d]",&a,&b,&c);改一下 scanf("%d:%d:%d",&a,&b,&c);

user: user-0
content: 引用 3 楼 这是一个正经昵称的回复:输入字符串，用char类型存储，用%s打印。为什么是看起来乱的数字的话，学一下char和int存储的区别，再学一下字符串是怎么存储的，还有地址跟数据。 #include<stdio.h>  int main() {     int t;     scanf("%d",&t);     for(int i=0; i<t; i++)     {         int n;         char s[100];         scanf("%s",s);         scanf("%d",&n);         /*这里错了         for(int p=0; p<n; p++)         {             int a,b,c;             scanf("[%d:%d:%d]",&a,&b,&c);             printf("%d %d %d",a,b,c);             printf("\n");         }         */     }     return 0; }

user: user-2
content: 引用 2 楼 Aa_1z 的回复:[quote=引用 1 楼 ctrigger的回复:]随便找一本C语言书，只看第一章即可。 ？第一章是引言[/quote]  用C语言标准库规定的格式，不能自创。你自创的，C语言不认识。

user: user-4
content: 输入字符串，用char类型存储，用%s打印。为什么是看起来乱的数字的话，学一下char和int存储的区别，再学一下字符串是怎么存储的，还有地址跟数据。

user: user-0
content: 引用 1 楼 ctrigger的回复:随便找一本C语言书，只看第一章即可。 ？第一章是引言

user: user-2
content: 随便找一本C语言书，只看第一章即可。