Implicit assumptions (was: Re: Some fun with -O2)

Walter von Entferndt walter.von.entferndt at posteo.net
Fri Jan 15 18:11:58 UTC 2021


At Freitag, 15. Januar 2021, 08:02:49 CET Mark Millard wrote:
> FYI: C itself has, in <limits.h> , CHAR_BIT for the number of bits in a
> Byte for how sizeof counts Bytes:  sizeof(char), sizeof(signed char),
> and sizeof(unsigned char) are each always 1.
> 
No, CHAR_BIT is the #bits in a *char*, which is the (standard) datatype for 
the binary representation of a character/letter/symbol in written human 
language, and for small integers.  The name also suggests that, as well as the 
comment in the header file.  That does not necessarily equal a "byte", which 
(by commonly accepted knowledge) is the smallest addressable entity in a 
computer's memory.  Of course, e.g. https://code-reference.com/c/datatypes/
char tells a *char* occupies one byte.  Sadly, AFAIK C itself does not define 
what a "byte" is, although that term is mentioned many times in reference 
manuals (implicit assumption).  So /theoretically/ CHAR_BIT and NBBY can 
differ.  In fact, many library funtions operating on characters/letters take 
an *int* instead of a *char* for performance reasons.  From https://code-reference.com/c/stdlib.h/sizeof: "the *sizeof* operator returns the number of 
bytes to be reserved for a variable or a data type".  Of course, for practical 
reasons, we can safely assume that a *char* will take one byte in storage 
space for the foreseeable future, since the consequences of changing that 
would be disastrous.
-- 
=|o)	"Stell' Dir vor es geht und keiner kriegt's hin." (Wolfgang Neuss)




More information about the freebsd-hackers mailing list