在分配字符多个字符 [英] assigning more than one character in char

查看:222
本文介绍了在分配字符多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个计划让输出'Y'

Why this program gives output 'y'

#include <stdio.h>

int main(void) {
    char ch='abcdefghijklmnopqrstuvwxy';
    printf("%c",ch);
    return 0;
}

code。在ideone

推荐答案

这是一个多字符文字。

文字普通字符包含不止一个C-char是一个
  多字符文字。一个多字符常量的类型为int和
  实施德科幻奈德值

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

Also from 6.4.4.4/10 in C11 specs

这是整型字符常量的类型为int。一个整数的值
  包含映射到单个字符字符常量
  单字节执行字符是的数值
  再PTED为整数的映射字符间$ P $的presentation。该
  包含多个整型字符常量的值
  字符(例如,'AB'),或包含字符或转义序列
  不映射到单字节执行字符,是
  实现定义。如果一个整型字符常量包含
  单个字符或转义序列,它的价值是导致一
  当char类型的值的对象是单一的
  字符或转义序列转换为int类型。

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.

所以行字符CH ='abcdefghijklmnopqrstuvwxy'您的系统中(假设4字节INT)可能编译为:

So the line char ch = 'abcdefghijklmnopqrstuvwxy' on your system (assuming 4 byte int) possibly compiles to:

char ch = 0x76777879;  // SOME int value (may be different, but documented in the compiler documents)

CH 将被赋予ABCDEF ... Y 其中的可能相当于 (INT)0x616263646566 ... 79 在ASCII编码和溢出的整数。这就是为什么 GCC 生成下列警告的原因:

ch will be assigned 'abcdef...y' which may be equivalent to (int)0x616263646566...79 in ascii encoding and overflows an integer. This is the reason why gcc generates the following warning:

multicharlit.c:在函数'主':
multicharlit.c:4:13:警告:
  字符常量太长,其类型[默认启用]

  multicharlit.c:4:5:警告:溢出隐不断转换
  [-Woverflow]

看来你的系统上,至少有显著8位用于分配给 CH 。因为你的性格文字是恒定的,这个最有可能发生在编译​​时(例如,​​当我编译如下情况发生 GCC

It appears on your system, least significant 8 bits are used to assign to ch. Because your character literal is constant, this most possibly happens at compile time: (For example following happens when I compile with 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;

}


此外偷一些善良<一个href=\"http://stackoverflow.com/questions/31335472/assigning-more-than-one-character-in-char#comment50655643_31335472\">unwind's评论

请记住,一个单引号字符常量的类型是 INT
  但你将其分配给字符,所以它必须被截断到
  单个字符。

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'型的例子是 INT C 。 (不要与混淆'A' C ++ 这是一个字符。在另一方面类型的'AB' INT 两个 C C>和 C ++

Type of 'a' for example is int in C. (Not to be confused with 'a' in C++ which is a char. On the other hand type of 'ab' is int in both C and C++.)

现在当你将这个 INT 键入一个字符类型和值超过了可以再$通过psented p $一个字符,然后一些压榨需要做的结果,适合少更宽键入字符和实际的结果是实现定义的。

Now when you assign this int type to a char type and value is more than that can be represented by a char, then some squeezing needs to be done to fit the result into less wider type char and the actual result is implementation-defined.

这篇关于在分配字符多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆