Get size of data types in C – Using sizeof operator

Example to calculate size of data types in C programming using sizeof() operator. The C program example demonstrates size of multiple data types  e.g. size of int, float, char, pointers, void pointer, int pointer, and char pointer etc.

NOTE:

  • Size of data types are platform dependent. So, size may varies i.e. on 32 and 64 bit platforms.
  • Size of all types of pointer in C e.g. int*, char* and void* will be same on one platform, because all types of pointers only stores address of data whatever it is int, float or char pointer.

Example of size of data types in C

Here’s the example program to find size of different data types and pointers.


/*Example program to find size of different types in C*/
#include <stdio.h>
int main()
{
	/*Declare multiple data types 
	* Create a list of data types
	* int => data types, a is variable
	*/

	int a;
	unsigned int b;
	short c;
	unsigned short d;
	long e;
	unsigned long f;
	char g;
	unsigned char h;
	signed char i;
	float j;
	double k;
	long double l;

	//Different type of pointers
	int* ip;
	float* fp;
	char* cp;
	void* vp;

	/* Get size of data types using sizeof operator */	

	//Examples - size of primitive data types
	printf("Size of primitive data types in bytes:\n");
	printf(" int size\t:%d\n" ,sizeof(a));
	printf(" unsigned int\t:%d\n",sizeof(b));
	printf(" short size\t:%d\n", sizeof(c));
	printf(" unsigned short\t:%d\n", sizeof(d));
	printf(" long	\t:%d\n", sizeof(e));
	printf(" unsigned long\t:%d\n", sizeof(f));
	printf(" char	\t:%d\n", sizeof(g));
	printf(" unsigned char\t:%d\n", sizeof(h));
	printf(" singed char\t:%d\n", sizeof(i));
	printf(" float size\t:%d\n",sizeof(j));
	printf(" double	\t:%d\n", sizeof(k));
	printf(" long double\t:%d\n",sizeof(l));

	//Examples - size of pointer of different types

	printf("\nSize of pointers in bytes:\n");

	printf(" int pointer\t:%d\n", sizeof(ip));
	printf(" float pointer\t:%d\n",sizeof(fp));
	printf(" char pointer\t:%d\n", sizeof(cp));
	printf(" void pointer\t:%d\n",sizeof(vp));

	return 0;
}

Output:
Size of primitive data types in bytes:
int size :4
unsigned int :4
short size :2
unsigned short :2
long :4
unsigned long :4
char :1
unsigned char :1
singed char :1
float size :4
double :8
long double :8

Size of pointers in bytes:
int pointer :4
float pointer :4
char pointer :4
void pointer :4

Related Posts