Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If CHAR_BIT != 8, what is the size of other types?

Tags:

c

sizeof

The C standard mandates sizeof(char) to be 1, no matter how many bits it actually takes.
Are other data-types measured in terms of bytes or chars in case these are not the same? Basically, assuming CHAR_BIT is 16, would sizeof(int16_t) be equal to 1 or 2?

like image 600
EOF Avatar asked Jan 12 '14 20:01

EOF


People also ask

What is the maximum number of bits in an ASCII character?

ASCII is a 7 bit character set. In C normally represented by an 8 bit char. If highest bit in an 8 bit byte is set, it is not an ASCII character.

How many bits are there in a byte?

These days, almost all architectures use 8 bits per byte (But it is not the case always, some older machines used to have 7-bit byte). It can be found in Let us see an application of it. Suppose we wish to print byte by byte representation of an integer.

How many bits of memory does a single character take up?

I've seen that the below program is taking only 7 bits of memory to store the character, but in general everywhere I've studied says that char occupies 1 byte of memory ie is 8 bits. Does a single character require 8 bits or 7 bits? If it requires 8 bits, what will be stored in the other bit?

What is the size of int on 64-bit computers?

sizeof (unsigned char) are equal to 1. What is the size of int on 64-bit computers ? range the standard requires for ints (-32767 through +32767). The only question actually is. Can char have minimal size different from signed char ?


1 Answers

Basically, assuming CHAR_BIT is 16, would sizeof(int16_t) be equal to 1 or 2

Size of objects (as yielded by sizeof operator) is measured in bytes and a byte in C has CHAR_BIT bits.

(C99, 6.2.6.1p4) "Values stored in non-bit-field objects of any other object type consist of n x CHAR_BIT bits, where n is the size of an object of that type, in bytes."

int16_t type if present has a width of exactly 16-bit and no padding. This means if CHAR_BIT == 16, then sizeof (int16_t) == 1

like image 89
ouah Avatar answered Oct 19 '22 17:10

ouah