1

I have a char array in C

char value_numbers [] = {'2', '3', '4', '5', '6', '7', '8', '9', '10'};

but I get the following error messages in XCode

Implicit conversion from 'int' to 'char' changes value from 12592 to 48
Multi-character character constant

Does anyone know what this means?

FrankS
  • 27
  • 1
  • 4
  • 10
    Well, there's no ''10" char. That's two chars. So it's going to interpret it as a "Multi-character character constant" which seems to have a value of `12592`, and that won't fit into `char`. You should post the rest of your program so we can see what you're trying to do and help you do it the right way. – Blaze Dec 17 '18 at 10:49
  • 1
    Possible duplicate of [Multi-character constant warnings](https://stackoverflow.com/questions/7755202/multi-character-constant-warnings) – izac89 Dec 17 '18 at 11:08

1 Answers1

1

12592 is 0x3130. That suggests that your C compiler represents characters with ASCII and sets the values of multiple-character character constants in a straightforward way, as if each character were a digit in a base-256 numeral.

To initialize an element of value_numbers with this value, the compiler must convert 12592 to a char. If char is unsigned, this is effectively done by taking just the low eight bits, which are 0x30 or 48, the code for '0'. (Mathematically, the remainder modulo 256 is taken.) If char is signed, the C standard requires the C implementation to define the result of converting the value (which may include signaling an exception instead of producing a value and continuing). Wrapping modulo 256 to a representable value is common.

Since your source code '10' represents the value 12592, but the compiler was forced to store a different value in the array, it warns you.

Note that the actual character encoding is implementation dependent (0 is 48 in ASCII but not in EBCDIC).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    It is overly broad to say this is undefined by the standard. “Undefined” means no requirements are imposed, but the C standard does have some rules that apply. For example, the C standard requires an implementation to define the values of multiple-character character constants. – Eric Postpischil Dec 17 '18 at 12:07
  • @EricPostpischil: What I meant is that the *charset* (character <-> code) is unspecified, and that the 12592 and 48 values were implementation dependant. But you are right, *undefined* in C is close to UB which is not the current use case. What should be the correct wording here? (I'm sorry, but English is not my first language and I often use one word when another one could be better) – Serge Ballesta Dec 17 '18 at 13:53
  • *implementation-defined* is the right terminology in both cases (the character set, and the value of a multi-character constant) – M.M Dec 18 '18 at 00:06