How to calculate size of a structure in C?

In C language, sizeof() operator is used to calculate the size of structure, variables, pointers or data types, data types could be pre-defined or user-defined. Using the sizeof() operator we can calculate the size of the structure straightforward to pass it as a parameter.

Let us see and example program to calculate the size of a structure:

/* C programs to understand how to calculate the size of a structure */
/* www.codewindow.in */

#include <stdio.h>
 
struct student {  // Declaring a structure named "student"
   int rollno;
   char name[16];
   int marks;
};
 
int main() {
   struct student s;  // Declaring a structure type data named "s"
   int size = sizeof(s);
   printf("Size of Structure : %d", size);
 
   return 0;
}

Output:

Size of Structure : 24

EXPLANATION:

1. Structure is Collection of elements of the Different data Types.
2. Size of the Structure Can be Evaluated using sizeof() Operator.

As the size of int = 4 bytes and size of char = 1 bytes in 64 bit compiler

Size of Structure ‘s’ = sizeof (rollno) + sizeof (name) + sizeof (mark)
= 4 + 16*1 + 4
= 24

You Missed

Also Checkout