gpt4 book ai didi

c - % [^\n] 的用法

转载 作者:行者123 更新时间:2023-12-02 00:40:27 24 4
gpt4 key购买 nike

A[50][5000];
for(i=0;i<50;++i)
scanf("%[\n]",A[i]);

%[^\n]
usage and meaning of it

and can i use that struct like

%[\t]
%[\a]

最佳答案

scanf()"%[" 转换说明符启动所谓的“扫描集”。它与看起来相同的正则表达式构造有一些相似之处(但仍然完全不同)这是标准所说的:

Matches a nonempty sequence of characters from a set of expected characters (the scanset).

...

The conversion specifier includes all subsequent characters in the format string, up to and including the matching right bracket (]). The characters between the brackets (the scanlist) compose the scanset, unless the character after the left bracket is a circumflex (^), in which case the scanset contains all characters that do not appear in the scanlist between the circumflex and the right bracket. If the conversion specifier begins with [] or [^], the right bracket character is in the scanlist and the next following right bracket character is the matching right bracket that ends the specification; otherwise the first following right bracket character is the one that ends the specification. If a - character is in the scanlist and is not the first, nor the second where the first character is a ^, nor the last character, the behavior is implementation-defined.

所以scanf()转换"%[\n]"会匹配一个换行符,而"%[^\n]" 将匹配所有字符直到换行符。

以下是 P.J. Plauger 在“标准 C 库”中对扫描集的评价:

A scan set behaves much like the s conversion specifier. It stores up to w characters (default is the rest of the input) in the char array pointed at by ptr. It always stores a null character after any input. It does not skip leading white-space. It also lets you specify what characters to consider as part of the field. You can specify all the characters that match, as in %[0123456789abcdefABCDEF], which matches an arbitrary sequence of hexadecimal digits. Or you can specify all the characters that do not match, as in %[^0123456789] which matches any characters other than digits.

If you want to include the right bracket (]) in the set of characters you specify, write it immediately after the opening [ (or [^), as in %[][] which scans for square brackets. You cannot include the null character in the set of characters you specify. Some implementations may let you specify a range of characters by using a minus sign (-). The list of hexadecimal digits, for example, can be written as %[0-9abcdefABCDEF] or even, in some cases, as %[0-9a-fA-F]. Please note, however, that such usage is not universal. Avoid it in a program that you wish to keep maximally portable.

关于c - % [^\n] 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com