Search code examples
ccharincludeundefined

C: CHAR_BIT do not change the char size


I recently founded CHAR_BIT and I tried to change it to 9 (or anything above 8)

#include <limits.h>

#define CHAR_BIT 9

Indeed when I print CHAR_BIT, it returns 9 (that's correct).

But when I want to use a char, thinking it will have a higher range, it does not. Somehow it is still with 8 bits.

Am I doing something wrong? Or it's just not possible to change the number of bits of a char? (I don't think so, but idk)

#include <stdio.h>
#include <limits.h>
#define CHAR_BIT 9

int main()
{
    unsigned char B = 260;
    printf("%d\n",B); //returns 4 (260-4->254, 8 bit char)
}

Solution

  • The number of bits in a byte is a property of the underlying hardware, and therefore cannot be changed.

    The CHAR_BIT macro just gives you a way to programmatically know what that value is.