32

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?

Let me explain. As I see it, there a 3 categories of systems:

  1. 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)
  2. 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)
  3. 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?

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?

15
  • 10
    @tbert sizeof(char) is always 1. It is not the size in bits, but rather in chars
    – Baruch
    Jul 22, 2012 at 12:45
  • 2
    no, it's the size of the type in bytes, from whence you can derive the number of bits.
    – tbert
    Jul 22, 2012 at 12:46
  • 2
    @tbert yes, it's size in bytes, but a byte is not always 8 bits. It's not the size in octets, which you would have meant IMO.
    – user529758
    Jul 22, 2012 at 12:48
  • 2
    @tbert, if sizeof(char) always returns 1, how does that help the OP? Jul 22, 2012 at 12:50
  • 4
    POSIX requires char=8bits. OTOH, some widely used DSPs have 16 or 32-bit chars, e.g. some TI ones used on many ARM platforms. Your smartphone may have one.
    – ninjalj
    Jul 22, 2012 at 12:53

5 Answers 5

29

use limits.h

CHAR_BIT

http://www.cplusplus.com/reference/clibrary/climits/

also, when you want to use exactly a given size, use stdint.h

8
  • 20
    I'd recommend this :-) #if (CHAR_BIT != 8) #error You are weird, go away! #endif Jul 22, 2012 at 12:58
  • 2
    I want to know if I can write code assuming char is 8 bits, not how to find the number of bits in a char
    – Baruch
    Jul 22, 2012 at 12:59
  • 1
    @baruch, maybe. Do you care? If you want to pack in 32 bits to an unsigned int and you are doing bit-twiddling, or using memcpy, memset, then yes you probably care. So in that case, use stdint.h types. If you are passing values to functions, or doing other stuff where you just want the native int type (or unsigned) used, then you probably don't care. Anytime you do actually care alot, then I would put a preprocessor guard somewhere that either warns the user they are entering no-mans land, or resolves the problem by providing two different implementations. Jul 22, 2012 at 13:03
  • @baruch, serialization is also an area where you have to be careful. Jul 22, 2012 at 13:13
  • @baruch, for these problems, lean on your compiler vendor and their standard implementation as much as possible. They've done most of the hard part for you. Also, if you do care about the number of bits in a byte, then I don't think it is possible to write 100% portable code. In that case you will probably want to write two implementations to take care of any differences between the two. This will be easier, faster, better than trying to write some convoluted mess that only half of the code will ever run on a given platform. Jul 22, 2012 at 13:16
6

For example, many DSP have CHAR_BIT greater than or equal to 16.

2
  • 1
    Wouldn't these fall into category 3 in my question?
    – Baruch
    Jul 22, 2012 at 13:02
  • 1
    @baruch: Not really, many of them are part of a larger system with a "conventional" CPU and an OS.
    – ninjalj
    Jul 22, 2012 at 13:14
4

At least, similar to the integer size in 64bit architectures, future platforms may use a wider char, with more bits. ASCII characters might become obsolete, replaced by unicode. This might be a reason so be cautious.

2
  • 1
    This is actually a counter example. In order to not break all the code relying on int being 32 bits, I think all common compilers leave int as 32 bits even on 64 bit systems.
    – Baruch
    Jul 22, 2012 at 12:55
  • 1
    @baruch, I agree they do so currently, however, who knows for how long.
    – perreal
    Jul 22, 2012 at 12:58
0

You can normally safely assume that files will have 8 bit bytes, or if not, that 8 bit byte files can be converted to a zero padded native format by a commonly-used tool. But it is much more dangerous to assume that CHAR_BIT == 8. Currently that is almost always the case, but it might not always be the case in future. 8 bit access to memory is increasingly a bottleneck.

1
  • 1
    If we all assume CHAR_BIT is equal to 8, then future processors will never be able to gain a foothold in the market because when we compile our programs to these processors, our programs will not work. Thus, CHAR_BIT will always be equal to 8. Haha? (actually, this makes me really depressed)
    – Jack G
    Dec 9, 2018 at 19:19
0

The Posix standards require CHAR_BIT to be 8.

So, if you only care about your code running on Posix compliant platforms, then assuming CHAR_BIT == 8 is fine and good.

The vast majority of commodity PC platforms and build systems comply with this requirement. Most any platform that uses the BSD socket interface likely implicitly has this requirement because the assumption that a platform byte is an octet is extremely widely distributed.

#if CHAR_BIT != 8
#error Your platform is unsupported!
#endif

Why did POSIX mandate CHAR_BIT==8?

You should only worry about this assumption / constraint if you want your code to run today on embedded and esoteric platforms. Otherwise, it's a pretty safe assumption in my view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.