C - scanf 的格式说明符?
C - format specifier for scanf?
float lat, lon;
char info[50];
scanf("%f, %f, %49[^\n]", &lat, &lon, info);
在上面的代码片段中,%49[^\n]
是什么格式说明符。
我知道这是字符数组的格式说明符,它将接受最多 49 个字符的输入(+ sentinal \0),并且 [^\n]
看起来像是一个正则表达式(虽然我有读到 scanf 不支持正则表达式的地方)或扩展为“任何其他字符”而不是“换行符”的字符集 \n
。我说得对吗?
另外,为什么写入数组信息的格式说明符中没有s
?
此代码段来自作品的程序。但这是好的C风格吗?
抱歉,我在下面的回答有误。如果你能跳到最后,我会给你正确答案。
*** 错误答案开始 ***
这不是正确的格式说明符,因为没有类型。
%[parameter][flags][width][.precision][length]type
是格式语句的规则。如您所见,type
是 non-optional。此格式项的作者认为他们可以将 regex
与 printf
结合起来,当两者具有完全不同的处理规则时(并且 printf
不遵循 regex
的模式)
*** 正确答案开始 ***
scanf
使用与 printf
不同的格式字符串规则 在 scanf
的手册页中是对 printf
规则
的补充
[
Matches a nonempty sequence of characters from the specified set
of accepted characters; the next pointer must be a pointer to char,
and there must be enough room for all the characters in the string,
plus a terminating null byte. The usual skip of leading white space is
suppressed. The string is to be made up of characters in (or not in) a
particular set; the set is defined by the characters between the open
bracket [ character and a close bracket ] character. The set excludes
those characters if the first character after the open bracket is a
circumflex (^). To include a close bracket in the set, make it the
first character after the open bracket or the circumflex; any other
position will end the set. The hyphen character - is also special;
when placed between two other characters, it adds all intervening
characters to the set. To include a hyphen, make it the last character
before the final close bracket. For instance, [^]0-9-] means the set
"everything except close bracket, zero through nine, and hyphen". The
string ends with the appearance of a character not in the (or, with a
circumflex, in) set or when the field width runs out.
这基本上意味着 scanf
可以使用 regex
规则的子集(字符集子集)进行扫描,但不能使用 regex
的所有规则
说明符 %[
是不同于 %s
的转换说明符,即使它也必须与类型 char *
(或 wchar_t *
)的参数配对。参见例如tablehere
[set] matches a non-empty sequence of character from set of characters.
If the first character of the set is ^, then all characters not in the set are matched. If the set begins with ] or ^] then the ] character is also included into the set. It is implementation-defined whether the character - in the non-initial position in the scanset may be indicating a range, as in [0-9]. If width specifier is used, matches only up to width. Always stores a null character in addition to the characters matched (so the argument array must have room for at least width+1 characters)
float lat, lon;
char info[50];
scanf("%f, %f, %49[^\n]", &lat, &lon, info);
在上面的代码片段中,%49[^\n]
是什么格式说明符。
我知道这是字符数组的格式说明符,它将接受最多 49 个字符的输入(+ sentinal \0),并且 [^\n]
看起来像是一个正则表达式(虽然我有读到 scanf 不支持正则表达式的地方)或扩展为“任何其他字符”而不是“换行符”的字符集 \n
。我说得对吗?
另外,为什么写入数组信息的格式说明符中没有s
?
此代码段来自作品的程序。但这是好的C风格吗?
抱歉,我在下面的回答有误。如果你能跳到最后,我会给你正确答案。
*** 错误答案开始 ***
这不是正确的格式说明符,因为没有类型。
%[parameter][flags][width][.precision][length]type
是格式语句的规则。如您所见,type
是 non-optional。此格式项的作者认为他们可以将 regex
与 printf
结合起来,当两者具有完全不同的处理规则时(并且 printf
不遵循 regex
的模式)
*** 正确答案开始 ***
scanf
使用与 printf
不同的格式字符串规则 在 scanf
的手册页中是对 printf
规则
[
Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex (^). To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [^]0-9-] means the set "everything except close bracket, zero through nine, and hyphen". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.
这基本上意味着 scanf
可以使用 regex
规则的子集(字符集子集)进行扫描,但不能使用 regex
的所有规则
说明符 %[
是不同于 %s
的转换说明符,即使它也必须与类型 char *
(或 wchar_t *
)的参数配对。参见例如tablehere
[set] matches a non-empty sequence of character from set of characters.
If the first character of the set is ^, then all characters not in the set are matched. If the set begins with ] or ^] then the ] character is also included into the set. It is implementation-defined whether the character - in the non-initial position in the scanset may be indicating a range, as in [0-9]. If width specifier is used, matches only up to width. Always stores a null character in addition to the characters matched (so the argument array must have room for at least width+1 characters)