Why is max length of C string literal different from max char[]?

后端 未结 3 1691
面向向阳花
面向向阳花 2020-12-06 10:31

Clarification: Given that a string literal can be rewritten as a const char[] (see below), imposing a lower max length on literals than on

3条回答
  •  抹茶落季
    2020-12-06 10:54

    It's not that 509 characters is the limit for a string, it's the minimum required for ANSI compatibility, as explained here.

    I think that the makers of the standard pulled the number 509 out of their ass, but unless we get some official documentation from this, there is no way for us to know.

    As far as how many characters can actually be in a string literal, that is compiler-dependent.

    Here are some examples:

    • MSVC: 2048
    • GCC: No Limit (up to 100,000 characters), but gives warning after 510 characters:

      String literal of length 100000 exceeds maximum length 509 that C90 compilers are required to support

提交回复
热议问题