Re: Is there a standard way to write an 8 bit byte?



Walter Roberson wrote:

I'm writing a simple image file exporter as part of a school project, and I would like to write completely platform-independent code if at all possible.


PBM -- Portable BitMap             (monochrome)
PNG -- Portable Network Graphics   (colour, lossless compression)

I'm actually using PNG.

The problem I've run into is that apparently there's no guarantee that a particular implementation will have a data type that is exactly 8 bits wide; char is only guaranteed to be at least 8 bits. Correct me if I'm wrong, but I assume that if CHAR_BIT happens to be, say, 9, writing a char into a file in binary mode would result in 9 bits being written.


Yes -- but what happens when you take that file to an 8-bit machine
is undefined.

That's exactly what I'm worried about.

Very few filesystems (none I -know- of) store files
as bitstreams: they are byte or word streams. Ethernet is bitstream,
but most well-known file-transfer protocols involve 8-bit characters.

Am I correct in deducing that if the file system stores files as byte streams, it doesn't matter how many bits wide char is as long as one doesn't try to write bytes with values that don't fit in an 8-bit byte?


Could there, as far as the standard is concerned, be a platform that uses 9-bit chars and stores files as bitstreams? :)


Kristian .



Relevant Pages

  • Re: Is there a standard way to write an 8 bit byte?
    ... > I would like to write completely platform-independent code if at all ... char is only guaranteed to be at least 8 bits. ... writing a char into a file in binary mode would result in 9 bits being ... There is no cast iron guarantee in the C standard that this will produce the ...
    (comp.lang.c)
  • Re: Use of Long and Long Long
    ... What the standard does guarantee is certain minimum *ranges* for the ... signed char is at least -127 .. ... that a type can have padding bits which don't contribute to the value. ...
    (comp.lang.c)
  • Re: Alignment on stack arrays
    ... when succeeding will initialize a to a pointer that is aligned at worst ... Do we have the guarantee that &ais always going to be aligned ... It will be correctly aligned for char on that system. ...
    (comp.lang.c)
  • Re: Whats the deal with the "toupper" family?
    ... char has no trap representations ... I consider 0 and -0 separate values for the purposes of my post. ... but I don't see a guarantee in the standard that signed or plain ...
    (comp.lang.c)