Is scanf's “regex” support a standard?

后端 未结 2 750
抹茶落季
抹茶落季 2021-01-01 15:58

Is scanf\'s \"regex\" support a standard? I can\'t find the answer anywhere.

This code works in gcc but not in Visual Studio:

scanf(\"%[^\\n]\",a);
<         


        
2条回答
  •  清酒与你
    2021-01-01 16:37

    The "%[" format spec for scanf() is standard and has been since C90.

    MSVC does support it.

    You can also provide a field width in the format spec to provide safety against buffer overruns:

    int main()
    {
        char buf[9];
    
        scanf("%8[^\n]",buf);
    
        printf("%s\n", buf);
        printf("strlen(buf) == %u\n", strlen(buf));
    
        return 0;
    }
    

    Also note that the "%[" format spec doesn't mean that scanf() supports regular expressions. That particular format spec is similar to a capability of regexs (and no doubt was an influenced by regex), but it's far more limited than regular expressions.

提交回复
热议问题