Memory Requirement of Different Data Types in C

 



When you write a C program, you’re actually telling your computer how to manage its memory. Every variable you create in C occupies some space in your computer’s memory, and the size depends on the data type you use. 

 In this lesson, we’ll explore the memory requirement of different data types in C and learn how to check them using the sizeof() operator.


Why Memory Requirement Matters? 

Imagine you’re packing for a trip . You wouldn’t use a big suitcase just to carry a toothbrush, right? Similarly, in programming, you should choose the right data type to save memory and improve efficiency.

 For example: 
 If you need to store someone’s age, use int.
 If you need to store the value of pi (3.14159), use float or double.  
 If you need to store a grade (like A, B, C), use char. 


Common Data Types and Their Sizes 

Here’s a quick look at the most commonly used data types in C:

Data Types in C

Data Type Typical Size (Bytes) Range (Approx)
char 1 -128 to 127 (signed) or 0 to 255 (unsigned)
int 4 -2,147,483,648 to 2,147,483,647
float 4 3.4E-38 to 3.4E+38 (up to 6 decimal places)
double 8 1.7E-308 to 1.7E+308 (up to 15 decimal places)
short 2 -32,768 to 32,767
long 4 or 8
(System Dependent)
Very large integer values
 These sizes are not fixed by the C language itself; they depend on the compiler and system architecture (32-bit vs 64-bit).


 Using sizeof() to Find Memory Requirements 

C provides the sizeof() operator, which lets you check how much memory a data type (or variable) occupies on your system. 

Example 1: Checking Sizes of Data Types 

#include <stdio.h>
int main() {
printf("Size of char: %lu byte(s)\n", sizeof(char));
printf("Size of int: %lu byte(s)\n", sizeof(int));
printf("Size of float: %lu byte(s)\n", sizeof(float));
printf("Size of double: %lu byte(s)\n", sizeof(double));
printf("Size of short: %lu byte(s)\n", sizeof(short));
printf("Size of long: %lu byte(s)\n", sizeof(long));
return 0;
}

***


 Sample Output (on a 64-bit system):

Size of char: 1 byte(s) 
 Size of int: 4 byte(s) 
 Size of float: 4 byte(s) 
 Size of double: 8 byte(s)
 Size of short: 2 byte(s) 
 Size of long: 8 byte(s)

Your system may give slightly different results depending on whether it’s 32-bit or 64-bit. 

 Example 2: Using sizeof() with Variables


#include <stdio.h>
int main() {
int age;
float pi;
char grade;
double salary;
printf("Size of age (int): %lu byte(s)\n", sizeof(age));
printf("Size of pi (float): %lu byte(s)\n", sizeof(pi));
printf("Size of grade (char): %lu byte(s)\n", sizeof(grade));
printf("Size of salary (double): %lu byte(s)\n",
sizeof(salary));
return 0;
}

 Sample Output:

Size of age (int): 4 byte(s) 
 Size of pi (float): 4 byte(s)
 Size of grade (char): 1 byte(s) 
 Size of salary (double): 8 byte(s)

 Notice that the size is determined by the data type of the variable, not its value. 

 Example 3: Comparing Memory Use

Let’s compare memory efficiency by storing the same kind of information with different data types.

#include <stdio.h>
#include <stdio.h>
int main() {
char a = 100; // stores 100 in 1 byte
int b = 100; // stores 100 in 4 bytes
double c = 100; // stores 100 in 8 bytes
printf("Size of a (char): %lu\n", sizeof(a));
printf("Size of b (int): %lu\n", sizeof(b));
printf("Size of c (double): %lu\n", sizeof(c));
return 0;
}

Sample Output:

Size of a (char): 1
 Size of b (int): 4 
 Size of c (double): 8 

 Although all three variables store the same value (100), they take different amounts of memory depending on the data type. 


  Final Thoughts 

● Every data type in C has a specific memory requirement. 
 ● Use the sizeof() operator to check memory sizes on your system. 
 ● Choosing the correct data type makes your program more efficient.
 ● Don’t use a big container (like double) if a small one (int or char) will do the job.

Previous Post Next Post