在 char 中分配多个字符
assigning more than one character in char
为什么这个程序给出输出'y'
#include <stdio.h>
int main(void) {
char ch='abcdefghijklmnopqrstuvwxy';
printf("%c",ch);
return 0;
}
这是一个多字符文字。
An ordinary character literal that contains more than one c-char is a
multicharacter literal . A multicharacter literal has type int and
implementation-defined value.
同样来自 C11 规范中的 6.4.4.4/10
An integer character constant has type int. The value of an integer
character constant containing a single character that maps to a
single-byte execution character is the numerical value of the
representation of the mapped character interpreted as an integer. The
value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined. If an integer character constant contains a
single character or escape sequence, its value is the one that results
when an object with type char whose value is that of the single
character or escape sequence is converted to type int.
因此您系统上的行 char ch = 'abcdefghijklmnopqrstuvwxy'
(假设 4 字节 int)可能编译为:
char ch = 0x76777879; // SOME int value (may be different, but documented in the compiler documents)
ch
在ascii编码中会被赋值为'abcdef...y'
which may be equivalent to(int)0x616263646566...79
并溢出一个整数。这就是 gcc
生成以下警告的原因:
multicharlit.c: In function ‘main’:
multicharlit.c:4:13: warning:
character constant too long for its type [enabled by default]
multicharlit.c:4:5: warning: overflow in implicit constant conversion
[-Woverflow]
它出现在您的系统上,最低有效 8 位用于分配给 ch
。因为您的字符文字是常量,所以这很可能发生在编译时:(例如,当我使用 gcc
编译时会发生以下情况)
$ cat multicharlit.c
#include <stdio.h>
int main(void) {
char ch='abcdefghijklmnopqrstuvwxy';
printf("%c",ch);
return 0;
}
$ gcc -O2 -fdump-tree-optimized multicharlit.c
$ cat multicharlit.c.143t.optimized
;; Function main (main) (executed once)
main ()
{
<bb 2>:
__builtin_putchar (121);
return 0;
}
还偷了
的一些好东西
Remember that the type of a single-quoted character constant is int
,
but you're assigning it to a char
, so it has to be truncated to a
single character.
例如 'a'
的类型是 C
中的 int
。 (不要与 C++
中的 'a'
混淆,后者是一个字符。另一方面,'ab'
的类型在 both[=58= 中是 int
] C
和 C++
.)
现在,当您将此 int
类型分配给 char
类型并且值大于 char
可以表示的值时,则需要进行一些压缩以适应结果变成更宽的类型char
,实际结果是实现定义的。
如果您打算打印出 abcdefghijklmnopqrstuvwxy,那么您应该将它存储到一个字符串变量而不是一个字符变量中 (char ch[50] = char abcdefghijklmnopqrstuvwxy;)。
字符串变量可以容纳多个字符,而字符变量只能容纳一个字符。
为什么这个程序给出输出'y'
#include <stdio.h>
int main(void) {
char ch='abcdefghijklmnopqrstuvwxy';
printf("%c",ch);
return 0;
}
这是一个多字符文字。
An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.
同样来自 C11 规范中的 6.4.4.4/10
An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.
因此您系统上的行 char ch = 'abcdefghijklmnopqrstuvwxy'
(假设 4 字节 int)可能编译为:
char ch = 0x76777879; // SOME int value (may be different, but documented in the compiler documents)
ch
在ascii编码中会被赋值为'abcdef...y'
which may be equivalent to(int)0x616263646566...79
并溢出一个整数。这就是 gcc
生成以下警告的原因:
multicharlit.c: In function ‘main’:
multicharlit.c:4:13: warning: character constant too long for its type [enabled by default]
multicharlit.c:4:5: warning: overflow in implicit constant conversion [-Woverflow]
它出现在您的系统上,最低有效 8 位用于分配给 ch
。因为您的字符文字是常量,所以这很可能发生在编译时:(例如,当我使用 gcc
编译时会发生以下情况)
$ cat multicharlit.c
#include <stdio.h>
int main(void) {
char ch='abcdefghijklmnopqrstuvwxy';
printf("%c",ch);
return 0;
}
$ gcc -O2 -fdump-tree-optimized multicharlit.c
$ cat multicharlit.c.143t.optimized
;; Function main (main) (executed once)
main ()
{
<bb 2>:
__builtin_putchar (121);
return 0;
}
还偷了
Remember that the type of a single-quoted character constant is
int
, but you're assigning it to achar
, so it has to be truncated to a single character.
例如 'a'
的类型是 C
中的 int
。 (不要与 C++
中的 'a'
混淆,后者是一个字符。另一方面,'ab'
的类型在 both[=58= 中是 int
] C
和 C++
.)
现在,当您将此 int
类型分配给 char
类型并且值大于 char
可以表示的值时,则需要进行一些压缩以适应结果变成更宽的类型char
,实际结果是实现定义的。
如果您打算打印出 abcdefghijklmnopqrstuvwxy,那么您应该将它存储到一个字符串变量而不是一个字符变量中 (char ch[50] = char abcdefghijklmnopqrstuvwxy;)。
字符串变量可以容纳多个字符,而字符变量只能容纳一个字符。