简体   繁体   English

为什么 16 位编译器会为 unsingned char[] 声明给出错误?

[英]Why 16-bit compiler gives error for unsingned char[] declaration?

Here is code:这是代码:

unsigned char A[] = { 'a', 'b', 12, 256, 'c', 28 };

it compiles well in VS using x64 compiler.它使用 x64 编译器在 VS 中编译良好。 But 16-bit compiler gives some error and unfortunately I don't know what kind of error.但是 16 位编译器给出了一些错误,不幸的是我不知道是哪种错误。 The question is why 16-bit gives error in this case.问题是为什么在这种情况下 16 位会出错。 Can you explain it?你能解释一下吗?

A char is always a byte, so whether your platform is 16-bit or 64-bit words doesn't really matter (though if you're using a system with CHAR_BIT != 8 , we'll talk!). char始终是一个字节,因此您的平台是 16 位还是 64 位字并不重要(尽管如果您使用的是CHAR_BIT != 8的系统,我们将讨论!)。 What's probably of more consequence is that your 16-bit compiler (yes, I'm assuming Turbo C++) is from the 1980s, a decade before the first standard edition of C++, so it behaves a bit differently overall.可能更重要的是您的 16 位编译器(是的,我假设是 Turbo C++)来自 1980 年代,比 C++ 的第一个标准版早十年,所以它的整体行为有点不同。

In this case, it is less tolerant of the value 256 , which is actually larger than can be stored in a char (signed or otherwise).在这种情况下,它对值256较低,该值实际上大于可以存储在char (有符号或其他方式)中的值。 I'd say it's "wrong", but it's hard to be non-compliant to a standard that didn't exist at the time.我会说这是“错误的”,但很难不遵守当时不存在的标准。 Turbo C++ is pretty much free to do its own thing, in that sense — it's not actually C++, in the way that we understand the term "C++" today.从这个意义上说,Turbo C++ 几乎可以自由地做自己的事情——它实际上不是 C++,就像我们今天理解的术语“C++”一样。

I would expect your Visual Studio compiler to emit a compiler warning... and then initialise the unsigned char using wraparound , as that's how unsigned values work.我希望您的 Visual Studio 编译器发出编译器警告......然后使用wraparound初始化unsigned char ,因为这就是无符号值的工作方式。

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

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