Multicharacter literal in C and C++

前端 未结 6 872
别那么骄傲
别那么骄傲 2020-11-22 03:30

I didn\'t know that C and C++ allow multicharacter literal: not \'c\' (of type int in C and char in C++), but \'tralivali\' (of type int!)

6条回答
  •  难免孤独
    2020-11-22 04:15

    I don't know how extensively this is used, but "implementation-defined" is a big red-flag to me. As far as I know, this could mean that the implementation could choose to ignore your character designations and just assign normal incrementing values if it wanted. It may do something "nicer", but you can't rely on that behavior across compilers (or even compiler versions). At least "goto" has predictable (if undesirable) behavior...

    That's my 2c, anyway.

    Edit: on "implementation-defined":

    From Bjarne Stroustrup's C++ Glossary:

    implementation defined - an aspect of C++'s semantics that is defined for each implementation rather than specified in the standard for every implementation. An example is the size of an int (which must be at least 16 bits but can be longer). Avoid implementation defined behavior whenever possible. See also: undefined. TC++PL C.2.

    also...

    undefined - an aspect of C++'s semantics for which no reasonable behavior is required. An example is dereferencing a pointer with the value zero. Avoid undefined behavior. See also: implementation defined. TC++PL C.2.

    I believe this means the comment is correct: it should at least compile, although anything beyond that is not specified. Note the advice in the definition, also.

提交回复
热议问题