Underlying type of character constants and \"plain char - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.10 Arithmetic Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=37) +---- Thread: Underlying type of character constants and \"plain char (/showthread.php?tid=324) |
Underlying type of character constants and \"plain char - Shane Swenson - 15-11-2006 We're wondering if rule 10.1 applies to character constants and the \"plain char\" type, and, if so, what their underlying types are. For example, Code: char_t c1 = 'a'; The underlying types of c1 and c2 are less clear. We could treat \"plain char\" similarly to signed char or unsigned char based on the implementation's signedness of \"plain char\", or from the discussion following rules 6.1 and 6.2, we could deduce that \"plain char\" is distinct from signed char and unsigned char with respect to rule 10.1. If rule 10.1 applies to character constants, we expect that the assignment to c2 violates rule 10.1 because it probably requires a conversion of the character constant to a narrower type. If the \"plain char\" type is unsigned on the target system, the assignment to c1 could be considered as violating rule 10.1 because it requires a conversion from a signed type to an unsigned type. If the \"plain char\" type is considered distinct from the signed char and unsigned char types, both assignments would violate rule 10.1 unless the underlying type of a character constant is also \"plain char\", though this also seems problematic since, for example, the plain char type usually can't represent the value 'abc'. What are the Committee's positions on these issues? - misra-c - 09-05-2007 All 10.x rules apply only to numeric expressions. The concept of underlying type will be fully expanded to cover plain char, enum, boolean and bit-fields in a future version. For now, consider the underlying type of 'a' to be plain char. - pkruk - 28-08-2007 Please clarify. ISO/IEC 9899:1999 C standard considers plain char type an \"integer type\": Quote:The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. Is my understanding correct, that we don't consider character constants an \"integer type\"? So this is also not a violation of 10.1 rule: Code: unsigned int c = 'x'; /* Not a \"conversion from signed to unsigned\" violation? */ I understand we still consider variables of signed/unsigned/plain char types as a \"integer types\"? Code: void foo(int x) { Re: - MMouse - 29-08-2007 pkruk Wrote:Please clarify. ISO/IEC 9899:1999 C standard considers plain char type an "integer type": It may do but the current MISRA-C references ISO/IEC 9899:1990 NOT 99 Question is should the next one look at C99? - misra-c - 26-09-2007 While C considers plain char, signed char and unsigned char to be integral types, MISRA-C considers plain char to be a distinct underlying type which is used only for \"character\" data. From your examples, Code: unsigned int c = 'x'; /* Not a \"conversion from signed to unsigned\" violation? */ This breaks rule 6.2. Code: void foo(int x) { This breaks 10.1. |