/*
How to avoid the structure padding in C?
The structural padding is an in-built process that is automatically done by the
compiler. Sometimes it required to avoid the structure padding in C as it makes the
size of the structure greater than the size of the structure members.
We can avoid the structure padding in C in two ways:
Using #pragma pack(1) directive
Using attribute
*/
#include <stdio.h>
#pragma pack(1)
struct base
{
int a;
char b;
double c;
};
int main()
{
struct base var; // variable declaration of type base
// Displaying the size of the structure base
printf("The size of the var is : %zu", sizeof(var));
return 0;
}
#include <stdio.h>
struct base
{
int a;
char b;
double c;
}__attribute__((packed));
int main()
{
struct base var; // variable declaration of type base
// Displaying the size of the structure base
printf("The size of the var is : %d", sizeof(var));
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: