Multi-character constant warnings

前端 未结 6 1503
别那么骄傲
别那么骄傲 2020-11-22 09:26

Why is this a warning? I think there are many cases when is more clear to use multi-char int constants instead of \"no meaning\" numbers or instead of defining const variabl

6条回答
  •  醉梦人生
    2020-11-22 09:57

    According to the standard (§6.4.4.4/10)

    The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.

    long x = '\xde\xad\xbe\xef'; // yes, single quotes
    

    This is valid ISO 9899:2011 C. It compiles without warning under gcc with -Wall, and a “multi-character character constant” warning with -pedantic.

    From Wikipedia:

    Multi-character constants (e.g. 'xy') are valid, although rarely useful — they let one store several characters in an integer (e.g. 4 ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one). Since the order in which the characters are packed into one int is not specified, portable use of multi-character constants is difficult.

    For portability sake, don't use multi-character constants with integral types.

提交回复
热议问题