Sizes of Char and Stream_Element is always equal?


? Interfaces.C.char’Size always equal to Ada.Stream.Stream_element’Size ?

If they are not the same, what are the most common sizes?


RM 13.13 says Stream_Element'Size is implementation defined. On all systems GCC 13/GNAT currently supports:

Ada.Streams line 42

type Stream_Element is mod 2 ** Standard'Storage_Unit;

Code search “Storage_Unit : constant”

Storage_Unit : constant := 8;

C’s char is a fuzzier concept, with no cross-platform size definition. GCC has several compile-time macros and flags that can alter the definition of char, for example -fsigned-char. As far as I can tell, none of those switches change the size to anything other than 8 bits.

GNAT defines char as:
Interfaces.C line 105

type char is new Character;

RM 3.5.2 Character Types limits Character to the 256 Latin-1 characters, but does not guarantee that the machine representation fits inside 8 bits.

However, GNAT uses the same internal representation as GCC’s C char for Character, so in practice it’s always 8 bits.

Other compilers/toolchains may do different things, maybe someone with a PTC Ada license can check the behavior there.


Very Very Very thanks!

GrateFull, Dani.

The absolutely correct answer is no.

The reasonable answer is yes.

Back in the late 1970’s when Ada was created, H/W had not yet standardized on the byte. DEC machines around that time had 36-bit words, for example. The ARM was therefore written to be H/W independent, and this has been maintained, so things like Storage_Unit and Stream_Element are implementation-defined. But now the byte is pretty much a standard, so unless you’re using unusual H/W, these things are all bytes.

The definition of things in Interfaces.C depend on the C compiler that the Ada compiler writer is interfacing to, but again, it’s rare for char to not be a byte.

1 Like


Very Very Very Thanks!

Gratefull, Dani.

there were also 7-bit bytes back then. Wasn’t there some other weird <8-bit and >8-bit (but <16) bytes too?

There are still some DSPs without byte addressing, where the smallest addressable memory datum is a “word”, for example 32 or 48 bits in size. A C compiler for such a machine is likely to use that size (32 or 48 bits) for char – I know of examples. An Ada compiler would likely make the same choice for Storage_Unit, but could have a smaller Stream_Element, at some cost in the size and speed of Streams code.