#include <stdio.h>

struct aa{
    char a,b;
    //int c;// see the difference in padding
    char d,e;
    //int c;// see the difference in padding
    };

int main(){
    struct aa aaa;
    printf("Size of this Structure is : %lu Bytes\n",sizeof(struct aa));
    //printf("Memory address of a-%d, b-%d, c-%d, d-%d, e-%d",&aaa.a,&aaa.b,&aaa.c,&aaa.d,&aaa.e);
}

#if 0
struct{
    int a;
    int b;   // 4 bytes each  
    int c;
}
total size = 12

struct{
    int a;   // so cansidering highest it stores 8bytes
    double b; //highest is 8bytes
    int c;   // 8bytes
}
total size = 24

struct{
    int d:5;// in 4bytes it only store in 1byte of 5bits -> 1 byte = 8bits
    int c:4;// int earliear 3 bytes were empty so it stores in 2nd byte
    //so last 2 bytes are left as padding
    int y; // here next 4bytes are considered
}
total size = 8
#endif

/*https://[Log in to view URL]

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

In fact important data structures like linked list,tree,etc. are implemented using
structures. Hence it becomes important that we use them efficiently. So, we can save
some memory space and make our programs efficient.

----------------------------------------------------------------------------------

*/

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: