FrankS
FrankS

Reputation: 27

C language: Implicit conversion from 'int' to 'char' changes value from 12592 to 48

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?

Upvotes: 1

Views: 4565

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148910

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).

Upvotes: 1

Related Questions