In C, structures are laid out nearly exactly as you specify in the code. Similar to C#'s StructLayout.Sequential.
The only difference is in member alignment. This never reorders the data members in a structure, but can change the size of the structure by inserting "pad" bytes in the middle of the structure. The reason for this is to make sure that each of the members starts on a boundary (usually 4 or 8 bytes).
For example:
struct mystruct {
int a;
short int b;
char c;
};
The size of this structure will usually be 12 bytes (4 for each member). This is because most compilers will, by default, make each member the same size as the largest in the structure. So the char will take up 4 bytes instead of one. But it is very important to note that sizeof(mystruct::c) will still by 1, but sizeof(mystruct) will be 12.
It can be hard to predict how the structure will be padded/aligned by the compiler. Most will default as I have explained above, some will default to no padding/alignment (also sometimes called "packed").
The method for altering this behavior is very compiler dependent, there is nothing in the language specifying how this is to be handled. In MSVC you would use #pragma pack(1)
to turn off alignment (the 1 says align everything on 1 byte boundaries). IN GCC you would use __attribute__((packed))
in the structure definition. Consult the documentation for your compiler to see what it does by default and how to change that behavior.