What is Structure Padding in C? - Examples

What is Structure Padding in C? - Examples
Techiio-author
Written by Shuvhojit DebFebruary 12, 2022
8 min read
C
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Let's discuss the structure padding in C.

About Structure Padding in C

Structure padding is a concept in C that adds one or more empty bytes between the memory addresses to align the data in memory.

Let's first understand the structure padding in C

Syntax:

struct student  
{  
  char a; // 1 byte  
  char b; // 1 byte  
  int c; // 4 bytes   
}  

Examples of Structure Padding in C

1) Example

Code:

//include basic C library files
#include <stdio.h>
//including string data member in C
#include <string.h>
//creating first structure
struct first_structure
{
int rollNo1, rollNo2;
char firstName;
char character;
float salary;
};
//creating second structure
struct second_structure
{
int rollNo1;
char firstName;
int rollNo2;
char character;
float salary;
};
//main method to run the C application
int main()
{
//taking first structure reference
struct first_structure s1;
//taking second structure reference
struct second_structure s2;
//displaying first_structure and second_structure output
printf("===================FIRST STRUCTURE===============================\n");
printf("size of first_structure in bytes : %d\n",sizeof(s1));
printf ( "\n   Address of rollNo1        = %u",&s1.rollNo1 );
printf ( "\n   Address of rollNo2        = %u",&s1.rollNo2 );
printf ( "\n   Address of firstName       = %u",&s1.firstName );
printf ( "\n   Address of character          = %u",&s1.character);
printf ( "\n   Address of salary = %u",&s1.salary);
printf("\n===================SECOND STRUCTURE===============================\n");
printf("size of second_structure in bytes : %d\n",sizeof(s2));
printf ( "\n   Address of rollNo1        = %u",&s2.rollNo1 );
printf ( "\n   Address of rollNo2        = %u",&s2.rollNo2 );
printf ( "\n   Address of firstName       = %u",&s2.firstName );
printf ( "\n   Address of character          = %u",&s2.character);
printf ( "\n   Address of salary = %u",&s2.salary);
getchar();
return 0;
}

Output:

blogpost

2) Example

Code:

//include basic C library files
#include<stdio.h>
//including string data member in C
#include <string.h>
//creating first structure
struct employee
{
char first_name[40];
char last_name[30];
};
//main method to run the C application
int main()
{
//taking first structure reference
struct employee e;
printf("Enter your first name:");
scanf("%s", &e.first_name);
printf("Enter your last name:");
scanf("%s",&e.last_name);
printf("First Name of Employee is :%s\n",e.first_name);
printf("Last Name of Employee is :%s\n", e.last_name);
//displaying output
printf("==================1ST STRUCTURE=========================\n");
printf("size of employee in bytes : %d\n",sizeof(e));
printf ( "\n   Address of first_name        = %u",&e.first_name);
printf ( "\n   Address of last_name        = %u",&e.last_name );
return 0;
}

Output:

blogpost

3) Example

Overcome Structure padding problem in C

Code:

//include basic C library files
#include <stdio.h>
//including string data member in C
#include <string.h>
#pragma pack(1)
//creating first structure
struct first_structure
{
int rollNo1, rollNo2;
char firstName;
char character;
float salary;
};
//creating second structure
struct second_structure
{
int rollNo1;
char firstName;
int rollNo2;
char character;
float salary;
};
//main method to run the C application
int main()
{
//taking first structure reference
struct first_structure s1;
//taking second structure reference
struct second_structure s2;
//displaying first_structure and second_structure output
printf("===================FIRST STRUCTURE===============================\n");
printf("size of first_structure in bytes : %d\n",sizeof(s1));
printf ( "\n   Address of rollNo1        = %u",&s1.rollNo1 );
printf ( "\n   Address of rollNo2        = %u",&s1.rollNo2 );
printf ( "\n   Address of firstName       = %u",&s1.firstName );
printf ( "\n   Address of character          = %u",&s1.character);
printf ( "\n   Address of salary = %u",&s1.salary);
printf("\n===================SECOND STRUCTURE===============================\n");
printf("size of second_structure in bytes : %d\n",sizeof(s2));
printf ( "\n   Address of rollNo1        = %u",&s2.rollNo1 );
printf ( "\n   Address of rollNo2        = %u",&s2.rollNo2 );
printf ( "\n   Address of firstName       = %u",&s2.firstName );
printf ( "\n   Address of character          = %u",&s2.character);
printf ( "\n   Address of salary = %u",&s2.salary);
getchar();
return 0;
}

Output:

blogpost

C Language
Programming
Structure Padding
Software
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance