便携式code - 每个字符位 [英] Portable code - bits per char
问题描述
我知道,C / C ++标准,既保证了的最小的每个字符8位,否则理论上9月16日/ 42 /一切皆有可能,而且因此有关编写可移植的所有站点code警告不要假设8bpc。我的问题是如何非便携这是真的?
I know that the C/C++ standards only guarantee a minimum of 8 bits per char, and that theoretically 9/16/42/anything else is possible, and that therefore all sites about writing portable code warn against assuming 8bpc. My question is how "non-portable" is this really?
让我解释一下。在我看来,有3类系统:
Let me explain. As I see it, there a 3 categories of systems:
- 计算机 - 我的意思是台式机,笔记本,服务器等运行Mac / Linux / Windows的/的Unix / * nix中/ POSIX /不管(我知道列表并不完全正确,但你的想法)。我会听到任何这样的系统,其中
字符
不是的究竟的8位非常惊讶。 (请纠正我,如果我错了) - 与操作系统设备 - 包括智能手机和这样的嵌入式系统。虽然我会不会很惊讶地发现这样的系统,其中
字符
更艾德里安8位,我还没有听到一个日期(再次,请通知我,如果我的只是不知道) - 裸机设备 - 录像机,微波炉,旧手机,等等。在这个领域我没有丝毫的经验,所以任何事情都有可能发生在这里。不过,我真的需要我的code是我的Windows桌面和我的微波炉之间的跨平台?难道我可能曾经有$ C $通用C到两者兼而有之?
- Computers - I mean desktops, laptops, servers, etc. running Mac/Linux/Windows/Unix/*nix/posix/whatever (I know that list isn't strictly correct, but you get the idea). I would be very surprised to hear of any such system where
char
is not exactly 8 bits. (please correct me if I am wrong) - Devices with operating systems - This includes smartphones and such embedded systems. While I will not be very surprised to find such a system where
char
is more tham 8 bits, I have not heard of one to date (again, please inform me if I am just unaware) - Bare metal devices - VCRs, microwave ovens, old cell phones, etc. In this field I haven't the slightest experience, so anything can happen here. However, do I really need my code to be cross platform between my Windows desktop and my microwave oven? Am I likely to ever have code common to both?
底线:是否有共同的(超过%0.001)平台(按类别1和2以上),其中字符
是的不的8位?而且是我上面的猜测是真的吗?
Bottom line: Are there common (more than %0.001) platforms (in categories 1&2 above) where char
is not 8 bits? And is my above surmise true?
推荐答案
使用limits.h中
use limits.h
CHAR_BIT
<一个href=\"http://www.cplusplus.com/reference/clibrary/climits/\">http://www.cplusplus.com/reference/clibrary/climits/
另外,如果你想准确使用一个给大小,使用stdint.h
also, when you want to use exactly a give size, use stdint.h
这篇关于便携式code - 每个字符位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!