简体   繁体   English

C语言:从“ int”到“ char”的隐式转换将值从12592更改为48

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

I have a char array in C 我在C中有一个char数组

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

but I get the following error messages in XCode 但是我在XCode中收到以下错误消息

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

Does anyone know what this means? 有人知道这意味着什么吗?

12592 is 0x3130. 12592是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. 这表明您的C编译器使用ASCII表示字符,并以一种直接的方式设置多字符字符常量的值,就好像每个字符都是以256为基数的数字一样。

To initialize an element of value_numbers with this value, the compiler must convert 12592 to a char . 要使用此值初始化value_numbers的元素,编译器必须将12592转换为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' . 如果char是无符号的,则可以有效地通过仅取低8位(即0x30或48,即'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). (在数学上,采用余数256。)如果对char进行了签名,则C标准要求C实现定义转换该值的结果(这可能包括发信号通知异常,而不是产生一个值并继续)。 Wrapping modulo 256 to a representable value is common. 通常将模256包装为可表示的值。

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. 由于源代码'10'表示值12592,但是编译器被迫在数组中存储其他值,因此会向您发出警告。

Note that the actual character encoding is implementation dependent (0 is 48 in ASCII but not in EBCDIC). 请注意,实际的字符编码取决于实现方式(0在ASCII中为48,但在EBCDIC中不是)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM