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



Kristian Nybo 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.

A laudable aim, and people here will be happy to help with anything that can be done in standard C.


> 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.

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.

In the simple case, yes. However, if the file is on a file system with 8 bit bytes it might have to do something very strange. Think about what could happen if the file was on a network share from a Windows box.


At first it seemed that stdint.h and its uint8_t were the answer, but then I noticed that uint8_t is only guaranteed to exist if the platform has a data type that is exactly 8 bits wide, which put me back in square one.

Correct.

Is there a way around this, or will I just have to use uint8_t and accept that my code might not work on every conceivable platform that has an ANSI C compiler?

You should try to isolate as much as possible the code that depending on having a type exactly 8 bits wide and use uint_8 for that. Possibly even putting it in a separate file.


Where you need at least 8 bits, but do not actually care how many bits, you could use uint_least8_t which will be 8 bits on systems that support it, but larger where not supported.

You should also understand that stdint.h was added by the C99 standard which is not supported by most compilers. However, it is easy enough to implement what you need of it on any given system, so I would still use them and implement them for any target that did not have stdint.h
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.




Relevant Pages

  • Re: Time Stuff
    ... > Platform: One you are an expert with. ... > about and then answer the questins, ... standard library. ... what support already exists. ...
    (comp.arch.embedded)
  • Re: Programming in standard c
    ... The standard already has many functions that wont ... multi-platform support in its internal implementation and provide that, ... think is appropriate for your platform. ... assumptions about those platform that you don't care about. ...
    (comp.lang.c)
  • Re: printf() and void *
    ... what the Standard calls a "strictly ... depends on the very characteristics of the platform that support its ... ability to produce output (if indeed it does produce output, ...
    (comp.lang.c)
  • Re: It is almost certain now, INTEL will have 64bit x86 !!
    ... So last time you looked AMD hadn't extended x86 to add 64bit ... I guess that could explain the standard of your postings on the ... OS vendors support. ... platform so AMD has that hat at the moment and unless Intel ...
    (comp.os.vms)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... These days I don't do compiler maintenance, but I HAVE done in the past, so ... You had a chance to point out how adherence to the standard can improve ... >> compile it WITH MINOR MODIFICATIONS for a specific platform. ... > platform-specific extension to COBOL ...
    (comp.lang.cobol)