EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Structure Padding in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Structure Padding in C

Structure Padding in C

Introduction to Structure Padding in C

Structure padding mainly talks about memory for variables which are aligned based on the size of the variable. Let suppose a “char” of 1 byte memory can be assigned anywhere in between like 0x5000 to 0x5001. Same way if we have an “int” of 4 bytes memory can be assigned anywhere in between like 0x5004 to 0x5008. This structure padding concept is automatic for its member are byte aligned by the compiler.

Pre-requisites: Padding, Structure member alignment and data packing.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How Does Structure Padding Work in C?

  • Structure padding is said to be in order to align the data in memory 1 or more un-occupied bytes (empty bytes) are kept between any memory addresses which are actually assigned for other data structure members at the time of memory allocation.
  • If we observe the architecture of the computer processor can be read 1 word means bytes in 32 bit processor from memory at a time.
  • Utilize this advantage of processor then data is always inserted as 4 bytes package which will becomes insert empty address spaces in between other existing members address.
  • After introducing this structure padding concept in we got to know that size of the structure is not always same.

Syntax:

Struct member{
Char character;
Int number;
Double salary;
}

Explanation: “Char” data type takes only 1 byte after 3 byte padding(Char, Int and Double) then the number will starts at 4 bytes boundary and rest “Int” and “Double” will takes 4 and 8 bytes respectively.

Examples of Structure Padding in C

Below are the different eamples of Structure Padding in C.

Example #1

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:

Structure Padding in C Output 1

Example #2

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:

Structure Padding in C Output 2

Example #3

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;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Structure Padding in C Output 3

Conclusion

Structure padding is said to be in order to align the data in memory 1 or more un-occupied bytes (empty bytes) are kept between any memory addresses which are actually assigned for other data structure members at the time of memory allocation.

Recommended Articles

This is a guide to Structure Padding in C. Here we discuss the working of Structure Padding in C along with different examples and code implementation. You can also go through our other related articles to learn more –

  1. Tokens in C
  2. Void Pointer in C
  3. Power Function in C
  4. Double Pointer in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More