Definition of Structures in C:

Structure is a collection of multiple variables of different types. Structures are used to create user-defined data types as a collection of other data types. For example, the co-ordination of location on earth consists of latitude and longitude. Let’s look at the syntax for this;

Enter the first name and the last name. The output will be as shown below:

Structure Padding in C:

Many processors expect memory for variables to be aligned based on the size of the variable. A ‘char’ of 1 byte can be allocated anywhere in memory like 0x5000 or 0x5001. And an ‘int’ of 4 bytes, must start at a 4-byte boundary like 0x5004 or 0x5008. The structure padding is automatically done by the compiler to make sure all its members are byte aligned.

The size of the below structure is 16 bytes due to structure padding:

Struct dummy {
Char ch;
Int num;
Double temp;
}

Here ‘char’ is only 1 byte but after 3 byte padding, the number starts at 4 byte boundary. For ‘int’ and ‘double’, it takes up 4 and 8 bytes respectively.

The compiler used the 3 wasted bytes (marked in red) to pad the structure so that all the other members are byte aligned. Now, the size of the structure is 4 + 1 +3 = 8 bytes. The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.