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

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

问题描述

为什么这个程序给出输出 'y'

Why this program gives output 'y'

#include <stdio.h>

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

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.整数的值包含单个字符的字符常量,该字符映射到单字节执行字符是映射字符的表示解释为整数.这包含多个字符的整数字符常量的值字符(例如,'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.

因此,您系统上的 char 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' 在 ascii 编码中 可能 等效于 (int)0x616263646566...79 并溢出一个整数.这就是 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:在函数main"中:
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;

}

<小时>

还从 unwind 的评论中窃取了一些好处


Also stealing some goodness from unwind's comment

记住单引号字符常量的类型是int,但是您将其分配给 char,因此必须将其截断为单个字符.

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' 的类型是 CC++ 中的 int.)

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 类型分配给 char 类型并且值超过 char 可以表示的值时,那么一些需要进行挤压以使结果适合更宽的类型 char 并且实际结果是实现定义的.

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.

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

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