Home / Programs / Write a program to calculate the size in bytes required by data items and in-built data types of C using the "sizeof" operator.
Programming Example

Write a program to calculate the size in bytes required by data items and in-built data types of C using the "sizeof" operator.

👁 2,049 Views
💻 Practical Program
📘 Step by Step Learning
Write a program to calculate the size in bytes required by data items and in-built data types of C using the "sizeof" operator.

Program Code

/*
 Program:  Write a program to calculate the size in bytes required by data
  items and in-built data types of C using the "sizeof" operator. 
  
 Author: www.atnyla.com  
 
*/ 

#include "stdio.h"  
int main()
{
	printf("char size = %d bytes\n", sizeof(char));
	printf("short size = %d bytes\n", sizeof(short));
	printf("int size = %d bytes\n", sizeof(int));
	printf("long size = %d bytes\n", sizeof(long));
	printf("float size = %d bytes\n", sizeof(float));
	printf("double size = %d bytes\n", sizeof(double));
	printf("1.55 size = %d bytes\n", sizeof(1.55));
	printf("1.55L size = %d bytes\n", sizeof(1.55L));
	printf("HELLO size = %d bytes\n", sizeof("HELLO"));
	return 0;
} 

Output

char size = 1 bytes
short size = 2 bytes
int size = 4 bytes
long size = 4 bytes
float size = 4 bytes
double size = 8 bytes
1.55 size = 8 bytes
1.55L size = 12 bytes
HELLO size = 6 bytes
Press any key to continue . . .

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.