Home > Software engineering >  Is int byte size fixed or it occupy it accordingly in C/C ?
Is int byte size fixed or it occupy it accordingly in C/C ?

Time:05-03

I have seen some program that use int instead of other type like int16_t or uint8_t even though there is no need to use int

let me give an example, when you assign 9 to an int, i know that 9 takes only 1 byte to store, so is other 3 bytes free to use or are they occupied?

all i'm saying is, does int always takes 4-bytes in memory or it takes byte accordingly and 4-bytes is the max-size

i hope you understand what im saying.

CodePudding user response:

The size of all types is constant. The value that you store in an integer has no effect on the size of the type. If you store a positive value smaller than maximum value representable by a single byte, then the more significant bytes (if any) will contain a zero value.

The size of int is not necessarily 4 bytes. The byte size of integer types is implementation defined.

CodePudding user response:

The size of types is fixed at compile time. There is no "dynamic resizing". If you tell the compiler to use int it will use an integer type that is guaranteed to have at least 16bit width. However, it may be (and is most of the time) more depending on the platform and compiler you are using. You can query the byte width on your platform by using sizeof(int).

There is a neat overview about the width of the different integer types at cppreference.

The int16_t or uint8_t are not core language types but convenience library defined types, that can be used if an exact bitwidth is required (e.g. for bitwise arithmetic)

CodePudding user response:

An int has no "free bytes". An int is at least 16 bits wide and the exact size depends on the target platform (see https://en.cppreference.com/w/cpp/language/types). sizeof(int) is a compile time constant though. It always occupies the same number of bytes, no matter what value it holds.

The fixed width integer types (https://en.cppreference.com/w/cpp/types/integer) are useful for code that assumes a certain size of integers, because assuming certain size of int is usually a bug. int16_t is exactly 16 bits wide and uint8_t is exactly 8 bits wide, independent of the target platform.

CodePudding user response:

I have seen some program that use int instead of other type like int16_t or uint8_t even though there is no need to use int

This is sometimes called "sloppy typing". int has the drawback that its size is implementation-defined, so it isn't portable. It can in theory even use an exotic signedness format (at least until the C23 standard).

when you assign 9 to an int, i know that 9 takes only 1 byte to store

That is not correct and there are no free bytes anywhere. Given some code int x = 9; then the integer constant 9 is of type int and takes up as much space as one, unless the compiler decided to optimize it into a smaller type. The 9 is stored in read-only memory, typically together with the executable code in the .text segment.

The variable x takes exactly sizeof(int) bytes (4 bytes on 32 bit systems) no matter the value stored. The compiler cannot do any sensible optimization regarding the size, other than when it is possible to remove the variable completely.

  • Related