Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a `char` always-always-always have 8 bits?

Tags:

c

memory

I've always assumed:

  1. that a char is represented by a byte,
  2. that a byte can always be counted upon to have 8 bits,
  3. that sizeof (char) is always 1,
  4. and that the maximum theoretical amount of memory I can allocate (counted in chars) is the number of bytes of RAM (+ swap space).

But now that I've read the Wikipedia entry on the byte I'm not so sure anymore.

Which one(s) of my assumptions is wrong? Which one(s) is dangerous?

like image 703
lindelof Avatar asked Mar 15 '12 20:03

lindelof


People also ask

Is a char 8-bit?

The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.

How many bits does a char have?

The smallest group of bits the language allows use to work with is the unsigned char , which is a group of 8 bits.

Is a char always 1 byte in C?

The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use. Here, a signed character is capable of holding negative values. Thus, the defined range here is -128 to +127.

Do bytes always have 8 bits?

The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures.


1 Answers

  1. Yes, char and byte are pretty much the same. A byte is the smallest addressable amount of memory, and so is a char in C. char always has size 1.

    From the spec, section 3.6 byte:

    byte

    addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

    And section 3.7.1 character:

    character

    single-byte character
    <C> bit representation that fits in a byte

  2. A char has CHAR_BIT bits. It could be any number (well, 8 or greater according to the spec), but is definitely most often 8. There are real machines with 16- and 32-bit char types, though. CHAR_BIT is defined in limits.h.

    From the spec, section 5.2.4.2.1 Sizes of integer types <limits.h>:

    The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

    — number of bits for smallest object that is not a bit-field (byte)
        CHAR_BIT                               8

  3. sizeof(char) == 1. Always.

    From the spec, section 6.5.3.4 The sizeof operator, paragraph 3:

    When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

  4. You can allocate as much memory as your system will let you allocate - there's nothing in the standard that defines how much that might be. You could imagine, for example, a computer with a cloud-storage backed memory allocation system - your allocatable memory might be practically infinite.

    Here's the complete spec section 7.20.3.3 The malloc function:

    Synopsis

    1 #include <stdlib.h>
       void *malloc(size_t size);

    Description

    2 The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

    Returns

    3 The malloc function returns either a null pointer or a pointer to the allocated space.

    That's the entirety of the specification, so there's not really any limit you can rely on.

like image 173
Carl Norum Avatar answered Sep 23 '22 03:09

Carl Norum