What is guaranteed by C-Standard has already been mentioned in other answers.
However, in order for it to b be at offset 1, your compiler could offer options to "pack" the structure, it will say that it explicitly adds no addition .
For gcc, this can be achieved with . #pragma pack()
#pragma pack(1)
struct
{
uint8_t a;
uint8_t b;
} test_packed;
#pragma pack()
struct
{
uint8_t a;
uint8_t b;
} test_unpacked;
A portable (and saved) solution would be to simply use an array:
struct
{
uint8_t ab[2];
} test_packed;
source
share