MISRA Discussion Forums

Full Version: Underlying type of character constants and \"plain char
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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';
char_t c2 = 'abc';
From the discussion in 6.10.4, we expect that the underlying types of 'a' and 'abc' are 8 bit signed and 32 bit signed, respectively, on a target with 8 bit chars.

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?
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.
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) {
  unsigned int c = x;  /* Violation: conversion from signed to unsigned. */
}
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?
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) {
  unsigned int c = x;  /* Violation: conversion from signed to unsigned. */
}


This breaks 10.1.