What is the purpose of using the [^ notation in scanf?

前端 未结 4 1539
后悔当初
后悔当初 2020-12-17 23:56

I have run into some code and was wondering what the original developer was up to. Below is a simplified program using this pattern:

      #include 

        
4条回答
  •  轮回少年
    2020-12-18 00:45

    You can read about it in the ISO/IEC9899 standard available online.

    Here is a paragraph I quote from the document about [ (Page 286):

    Matches a nonempty sequence of characters from a set of expected characters.

    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.

提交回复
热议问题