In this article, we are going to learn how to Find the Size of int, float, double, and char data types. Each data type in a programming language has its size associated with it. That size is allocated on memory when we declare that type of the variable on heap or stack.
1. C Program to Find the Size of Variables
In this example, we are going to find the size of integer, float, double, and character data types. We will also find the size of a structure.To find the size of each variable type and structure object we will use the sizeof() function.
sizeof(): The sizeof() function evaluates the number of bytes that a particular type of variable holds when it’s declared.
let us jump to the program below to see the size of the variables.
- “Hello, World!” Program in C Language
- C Program to take input from users and print
- C Program to Add or Subtract two Integers
- C Program to Multiply and Divide Two Numbers
- C Program to Find Size of int, float, double, char, structure
- C Program to Find ASCII Value of Character
#include<stdio.h>
struct{
int num1;
int num2;
char ch;
}abc;
int main()
{
int intnum;
float floatnum;
double doublenum;
char charstr;
printf("The Size of integer : %lu bytes\n", sizeof(intnum));
printf("The Size of float : %lu bytes\n", sizeof(floatnum));
printf("The Size of double : %lu bytes\n", sizeof(doublenum));
printf("The Size of char : %lu byte\n", sizeof(charstr));
printf("The Size of structure: %lu byte\n", sizeof(abc));
return 0;
}
Output
The Size of integer : 4 bytes
The Size of float : 4 bytes
The Size of double : 8 bytes
The Size of char : 1 byte
The Size of structure: 12 byte