Program to Calculate Size of Datatypes in C | Basic Datatypes Size in C
Program Introduction:
Write a C program to calculate the size of datatypes in C Programming language. We will calculate the all in-built datatypes size.
Program Description:
We are going to use the in-built C language sizeof operator to calculate the size of each datatype.
Syntax of sizeof operator:
1 |
sizeof(<variable_name>) |
sizeof operator will take variable as the input and returns the size of that variable (Means size of the variable datatype)
📢 sizeof is an operator but it looks like an function.
Size of the datatypes is compiler dependent. Means depending upon the compiler the size of the datatype will change. For example, Few compilers allocate 2 Bytes for the Integer. And few compilers allocate 4 Bytes for the Integer.
The size is depends on the compiler and compiler memory model implementation. So The output which I got for this program is dependent on my compiler. And your output might be differ from my output.
Program: Size of C Datatypes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<stdio.h> int main() { int i; char ch; long int li; float f; double d; long double ld; printf("Size of all Datatypes \n"); printf("Size of Integer Datatype is = %d \n", sizeof(i)); printf("Size of Character Datatype is = %d \n", sizeof(ch)); printf("Size of Long Integer Datatype is = %d \n", sizeof(li)); printf("Size of Float Datatype is = %d \n", sizeof(f)); printf("Size of Double Datatype is = %d \n", sizeof(d)); printf("Size of Long Double Datatype is = %d \n", sizeof(ld)); return 0; } |
Output Size of C Datatypes Program:
📢 The size of datatypes are compiler dependent. So the output which i have got on my machine might be different from your machine.
1 2 3 4 5 6 7 8 9 |
$ ./a.out Size of all Datatypes Size of Integer Datatype is = 4 Size of Character Datatype is = 1 Size of Long Integer Datatype is = 8 Size of Float Datatype is = 4 Size of Double Datatype is = 8 Size of Long Double Datatype is = 16 $ |
Conclusion:
In this article, We have written a program to calculate the size of all datatypes in the c programming language.
2 Responses
[…] Program to Calculate Size of Datatypes in C […]
[…] Program to Calculate Size of Datatypes in C […]