字符值 = 'abcd'.使用多字符 char

char val = #39;abcd#39;. Using multi character char(字符值 = abcd.使用多字符 char)

本文介绍了字符值 = 'abcd'.使用多字符 char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编译器如何处理具有多个字符的 char 变量感到困惑.我知道一个 char 是 1 个字节,它可以包含一个字符,如 ASCII.

但是当我尝试时:

char _val = 'ab';char _val = 'abc';char _val = 'abcd';

它们编译得很好,当我打印 _val 时,它总是打印最后一个字符.但是当我这样做时

char _val = 'abcde';

然后我得到一个编译器错误:

<块引用>

错误 1 ​​错误 C2015:常量中的字符过多

所以我的问题是:

  1. 为什么编译器在使用多个字符时总是取最后一个字符?这种情况下的编译器机制是什么.
  2. 为什么我在输入 5 个字符时会收到太多字符错误.2 个字符比 char 可以处理的要多,为什么要 5 个?

我正在使用 Visual Studio 2013.

谢谢.

解决方案

[lex.ccon]/1:

<块引用>

包含多个 c-char 的普通字符文字是多字符文字.多字符文字 [..] 是有条件支持的,类型为 int,并且具有实现定义的值.

<小时><块引用>

为什么编译器在多个时总是取最后一个字符使用字符?这种情况下的编译器机制是什么.

大多数编译器只是将字符值按顺序移动在一起:这样最后一个字符占据最低有效字节,倒数第二个字符占据紧邻最低有效字节的字节,依此类推.
IE.'abc' 等价于 'c' + ((int)'b')<<8) + (((int)'a')<<16)(演示).

将此 int 转换回 char 将有一个实现定义的值 - 这可能只是通过取 int 模的值而出现256. 那只会给你最后一个字符.

<块引用>

为什么我在输入 5 个字符时会收到太多字符错误.2字符比 char 可以处理的要多,为什么要 5 个?

因为在您的机器上,int 可能有四个字节大.如果上面确实是您的编译器排列多字符常量的方式,他不能将五个 char 值放入一个 int.

I have a confusion of how the compiler handles a char variable with multiple characters. I understand that a char is 1 byte and it can contain one character like ASCII.

But when I try:

char _val = 'ab';
char _val = 'abc';
char _val = 'abcd';

They compiles fine and when I print _val it always prints the last character. But when I did

char _val = 'abcde';

Then I got a compiler error:

Error 1 error C2015: too many characters in constant

So my questions are:

  1. Why does the compiler always takes the last character when multiple characters are used? What is the compiler mechanism in this situation.
  2. Why did I get a too many characters error when I put 5 characters. 2 characters is more than what a char can handle so why 5?

I am using Visual Studio 2013.

Thank you.

解决方案

[lex.ccon]/1:

An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal [..] is conditionally-supported, has type int, and has an implementation-defined value.


Why does the compiler always takes the last character when multiple characters are used? What is the compiler mechanism in this situation.

Most compilers just shift the character values together in order: That way the last character occupies the least significant byte, the penultimate character occupies the byte next to the least significant one, and so forth.
I.e. 'abc' would be equivalent to 'c' + ((int)'b')<<8) + (((int)'a')<<16) (Demo).

Converting this int back to a char will have an implementation defined value - that might just emerge from taking the value of the int modulo 256. That would simply give you the last character.

Why did I get a too many characters error when I put 5 characters. 2 characters is more than what a char can handle so why 5?

Because on your machine an int is probably four bytes large. If the above is indeed the way your compiler arranges multicharacter constants in, he cannot put five char values into an int.

这篇关于字符值 = 'abcd'.使用多字符 char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:字符值 = 'abcd'.使用多字符 char

基础教程推荐