ben
ben

Reputation: 6023

Is the size of built-in datatypes in c++ managed on source code level?

When I open /usr/include/stdint.h I see things like

typedef signed char   int8_t;

which means that every int8_t is to be treated like signed char. This let's me suspect that on my system signed char is of size 8bit. (However the other way round would be more intuitive for me i.e. every signed char has to be treated like int8_t.) Where is the size of signed char defined?

Upvotes: 1

Views: 50

Answers (3)

James Kanze
James Kanze

Reputation: 153919

It's not. It's only guaranteed to be at least 8 bits (since it must be able to contain values in the range [-127, 127]. And int8_t is also not guaranteed to exist; it's only present on machines where char is an 8 bit 2's complement.

Upvotes: 0

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

The implementation of the compiler for a particular architecture chooses whatever authors wanted for the "normal" datatype sizes (within the limits). So in a way, it's set in code, but not in the part visible to your programs in any means.

Then it's formulated in terms of the standard types, so that the sizes match.

Upvotes: 0

Paul Roub
Paul Roub

Reputation: 36438

Short answer: no.

The sizeof fundamental types like char (and therefore signed char) is defined by your compiler, based on the target system architecture. They're not defined in code.

The typedef above means the opposite - it's defining a new type named int8_t, in terms of the predefined char type. On your system (as on most) char is 8 bits wide, so it's the natural way to define an 8-bit integer type.

Upvotes: 1

Related Questions