How to Get the Size of a Structure in C

Embedded C for microcontrollers



In this article, we go over how to get the size of a structure in C.

The size will tell us how many bytes make up the structure.

A structure is a data element in C that can be composed of elements of different data types. It is used to create user-defined data types and allows us to combine data of different types.

As an example, let's go over creating a data structure representing a house.

We will have data elements, including the house size, house price, number of bedrooms, number of bathrooms, and whether the house has a pool or not.

These data elements will be of different types. The house size will be of a 32-bit integer. The house price will be of a 32-bit integer size. The number of bedrooms will be an 8-bit integer. The number of bathrooms will be an 8-bit integer. The presence of a pool will be a boolean variable for true or false.

So below we create a structure and initialize it with the example above. We then access the elements by using the name of the struct element followed by a period and the individual data element.



So we create a structure data type called House.

This is composed of several elements including housePrice (uint32_t), houseSize (uint16_t), houseBedrooms (uint8_t), houseBathrooms, and pool (bool).

The struct data type is normally declared outside of the main function and may even be placed in the header file.

We then have our main function.

This is the place, however, where we initialize the struct data type.

We create a struct element that is of type House; we name it redHouse.

The house size is 1200 (square feet), the house price is $250000, the number of bedroom is 2, the number of bathroom is 2, and there is no pool, so we set the value equal to false.

We then create a variable, size_of_structure, which gets the size of the redHouse. This is done using the sizeof() function, passing in the redHouse struct as its parameter.

The output of this program is shown below.





This means that the size of the structure object, redHouse, is 12 bytes.

Let's look at this program in more detail.

The structure object is made up of 5 variables.

The housePrice variable is made up of 4 bytes.

The houseSize variable is made up of 2 bytes.

The houseBedrooms and houseBathrooms variables are made up of 1 byte each for a total of 2 bytes.

The pool variable is made up of 1 byte.

This gives a total of 9 bytes.

However, the program gives us a total of 12 bytes.

This is because padding is added to the total byte usage because of the nature that they are made up of different data types and where a data element starts and stops in memory address location.

If we were to create a structure where all the elements have the same data types, then we could predictably calculate the byte size of the structure based on the elements.

This is shown in the code below.



Running this program, we get the following output shown below.



Since there are 4 variables and each variable is a 32-bit integer, each consumes 4 bytes, so the total byte usage of the structure is 16 bytes.

Because all of the variables are the same, there is no padding because there aren't different data types that require padding to offset the different types.

So this is something to keep in mind that when you have different data types in a structure, that there may be a greater byte usage than the total of the individual elements of the structure.

So this is how to get the size of a structure in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C



HTML Comment Box is loading comments...