C programming: Find the Size of int, float, double, char, pointers and structure

C programming: sizeof() operator

The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most sytems, sizeof( int ) would yield 4, as would sizeof( number ) if number were a variable of type int.

Examples of operator sizeof():


Example: Using directory and sub-directories in C

#include <string.h>
#define MAX 20

/* A structure representing a record in a student database */
struct Student
    char *f_name;// first name
    char l_name[20];// last name
    int id_number;	// social security number
    double grade_point;	// grade point average

int main(){
  //structure declarations
  struct Student s1;
  strcpy(s1.f_name, "Rosa");
  strcpy(s1.l_name, "Rowe");
  s1.id_number = 123;
  s1.grade_point = 88.89;
  struct Student s2;      
  struct Student *s3 = &s2; 
  struct Student s4[10];
  //"regular" datatype declarations
  char a, b[10], *p1 = b;
  int c, d[10], *p2 = d;
  double e, f[10], *p3 = f;
  printf("Size of for regular datatype:");
  printf("\nSize of 'char a' = %d Byte(s).",sizeof(a));
  printf("\nSize of 'char b[10]' = %d Byte(s).",sizeof(b));
  printf("\nSize of 'int c' = %d Byte(s).",sizeof(c));
  printf("\nSize of 'int d[10]' = %d Byte(s).",sizeof(d));
  printf("\nSize of 'double e' = %d Byte(s).",sizeof(e));
  printf("\nSize of 'double f[10]' = %d Byte(s).",sizeof(f));
  printf("\n\nSize of for pointers to regular datatypes:");
  printf("\nSize of 'char p1' = %d Byte(s).",sizeof(p1));
  printf("\nSize of 'char *p1' = %d Byte(s).",sizeof(*p1));
  printf("\nSize of 'int p2' = %d Byte(s).",sizeof(p2));
  printf("\nSize of 'int *p2' = %d Byte(s).",sizeof(*p2));
  printf("\nSize of 'double p3' = %d Byte(s).",sizeof(p3));
  printf("\nSize of 'double *p3' = %d Byte(s).",sizeof(*p3));
  printf("\n\nSize of for structures:");
  printf("\nSize of (s1) = %d Byte(s).", sizeof(s1));
  printf("\nSize of (s2) = %d Byte(s).", sizeof(s2));
  printf("\nSize of (s3) = %d Byte(s).", sizeof(s3));
  printf("\nSize of (s4) = %d Byte(s).", sizeof(s4));
  printf("\n\nSize of for pointers to structure members:");
  printf("\nSize of f_name = %d Byte(s).", sizeof(s1.f_name));
  printf("\nSize of l_name = %d Byte(s).", sizeof(s1.l_name));
  printf("\nSize of ID number = %d Byte(s).", sizeof(s1.id_number));
  printf("\nSize of grade point = %d Byte(s).", sizeof(s1.grade_point));

  return 0;


Size of for regular datatype:
Size of 'char a' = 1 Byte(s).
Size of 'char b[10]' = 10 Byte(s).
Size of 'int c' = 4 Byte(s).
Size of 'int d[10]' = 40 Byte(s).
Size of 'double e' = 8 Byte(s).
Size of 'double f[10]' = 80 Byte(s).

Size of for pointers to regular datatypes:
Size of 'char p1' = 8 Byte(s).
Size of 'char *p1' = 1 Byte(s).
Size of 'int p2' = 8 Byte(s).
Size of 'int *p2' = 4 Byte(s).
Size of 'double p3' = 8 Byte(s).
Size of 'double *p3' = 8 Byte(s).

Size of for structures:
Size of (s1) = 40 Byte(s).
Size of (s2) = 40 Byte(s).
Size of (s3) = 8 Byte(s).
Size of (s4) = 400 Byte(s).

Size of for pointers to structure members:
Size of f_name = 8 Byte(s).
Size of l_name = 20 Byte(s).
Size of ID number = 4 Byte(s).
Size of grade point = 8 Byte(s).

Follow us on Facebook and Twitter for latest update.