N
N
Nikc77772021-06-23 20:15:19
C++ / C#
Nikc7777, 2021-06-23 20:15:19

How many bits does the CHAR_BIT macro have?

In many sources it is written that the CHAR_BIT macro has one eight-bit byte. In the example below, this is not entirely true, rather not true at all.

#include <stdio.h>
#include <limits.h>

int main(void){
  char size = CHAR_BIT * sizeof(char);
  printf("%lu\n", sizeof(char));
  printf("%lu\n", sizeof(CHAR_BIT));
  printf("%u\n", size);
  return 0;
}

byte 1 
   4 
   8

Is it possible to use the sizeof() function, in cases like this, to check or believe the standards that CHAR_BIT has eight bits.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-06-23
@galaxy

CHAR_BIT - macro, the number of bits in a char. The standard is at least 8 (but it may well be more).
sizeof(char)= 1 by definition (sizeof measures everything in chars)
sizeof(CHAR_BIT)- given that CHAR_BIT is defined something like #define CHAR_BIT 8, here you calculatesizeof(int)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question