Type to use to represent a byte in ANSI (C89/90) C

2020-01-25 06:56发布

Is there a standards-complaint method to represent a byte in ANSI (C89/90) C? I know that, most often, a char happens to be a byte, but my understanding is that this is not guaranteed to be the case. Also, there is stdint.h in the C99 standard, but what was used before C99?

I'm curious about both 8 bits specifically, and a "byte" (sizeof(x) == 1).

标签: c types c89
6条回答
叛逆
2楼-- · 2020-01-25 07:44

In ANSI C89/ISO C90 sizeof(char) == 1. However, it is not always the case that 1 byte is 8 bits. If you wish to count the number of bits in 1 byte (and you don't have access to limits.h), I suggest the following:

unsigned int bitnum(void) {
    unsigned char c = ~0u; /* Thank you Jonathan. */
    unsigned int v;

    for(v = 0u; c; ++v)
        c &= c - 1u;
    return(v);
}

Here we use Kernighan's method to count the number of bits set in c. To better understand the code above (or see others like it), I refer you to "Bit Twiddling Hacks".

查看更多
做自己的国王
3楼-- · 2020-01-25 07:47

char is always a byte , but it's not always an octet. A byte is the smallest addressable unit of memory (in most definitions), an octet is 8-bit unit of memory.

That is, sizeof(char) is always 1 for all implementations, but CHAR_BIT macro in limits.h defines the size of a byte for a platform and it is not always 8 bit. There are platforms with 16-bit and 32-bit bytes, hence char will take up more bits, but it is still a byte. Since required range for char is at least -127 to 127 (or 0 to 255), it will be at least 8 bit on all platforms.

ISO/IEC 9899:TC3

6.5.3.4 The sizeof operator

  1. ...
  2. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. [...]
  3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]

Emphasis mine.

查看更多
smile是对你的礼貌
4楼-- · 2020-01-25 07:50

Before C99? Platform-dependent code.

But why do you care? Just use stdint.h.

In every implementation of C I have used (from old UNIX to embedded compilers written by hardware engineers to big-vendor compilers) char has always been 8-bit.

查看更多
可以哭但决不认输i
5楼-- · 2020-01-25 07:55

I notice that some answered have re-defined the word byte to mean something other than 8 bits. A byte is 8 bits, however in some c implementations char is 16 bits (2 bytes) or 8 bits (1 byte). The people that are calling a byte 'smallest addressable unit of memory' or some such garbage have lost grasp of the meaning of byte (8 bits). The reason that some implementations of C have 16 bit chars (2 bytes) and some have 8 bit chars (1 byte), and there is no standard type called 'byte', is due to laziness.

So, we should use int_8

查看更多
▲ chillily
6楼-- · 2020-01-25 08:01

You can always represent a byte (if you mean 8bits) in a unsigned char. It's always at least 8 bits in size, all bits making up the value, so a 8 bit value will always fit into it.

If you want exactly 8 bits, i also think you'll have to use platform dependent ways. POSIX systems seem to be required to support int8_t. That means that on POSIX systems, char (and thus a byte) is always 8 bits.

查看更多
放我归山
7楼-- · 2020-01-25 08:01

You can find pretty reliable macros and typedefs in boost.

查看更多
登录 后发表回答