Introduction
In our previous chapter, we learned that a variable is like a storage box inside your computer's memory (RAM). However, in the real world, you would not use a massive shipping container to store a single diamond ring, nor would you use a tiny shoebox to store a refrigerator. You choose the box size based on what you need to put inside it.
The C compiler thinks the exact same way. Before it gives you a storage box (variable) in memory, it demands to know exactly what kind of data you plan to put inside it. Will it be a small number? A large decimal? A single alphabet letter?
This is where Data Types come into play. In this chapter, we will explore the four primary data types in C, understand exactly how much memory they consume, and learn how to use the powerful sizeof() operator.
What are Primitive Data Types?
C is a statically typed language. This means that once you declare a variable with a specific data type, it can never hold any other type of data. If you create a box for integers, you cannot suddenly put text into it.
The core building blocks of data in C are called primitive data types. There are four main ones every programmer must know:
1. Integer (int)
The int data type is used to store whole numbers without any decimal points. They can be positive, negative, or zero.
- Example:
int age = 25; - Memory Size: Typically consumes 4 bytes of RAM.
- Format Specifier:
%d
2. Floating-Point (float)
When you need to store numbers that have a fractional or decimal part, you use float. This is perfect for percentages, temperatures, or currency.
- Example:
float temperature = 98.6; - Memory Size: Consumes 4 bytes of RAM.
- Precision: Accurate up to 6 or 7 decimal places.
- Format Specifier:
%f
3. Double Precision (double)
A double is just like a float, but it provides double the precision and takes up double the memory space. When scientists calculate planetary orbits or bankers calculate massive financial compound interest, they use doubles instead of floats to avoid rounding errors.
- Example:
double pi = 3.14159265359; - Memory Size: Consumes 8 bytes of RAM.
- Precision: Accurate up to 15 decimal places.
- Format Specifier:
%lf
4. Character (char)
The char data type is used to store a single character, such as a letter, a number, or a special symbol. In C, single characters must always be wrapped in single quotes, not double quotes.
- Example:
char grade = 'A'; - Memory Size: Consumes exactly 1 byte of RAM.
- Format Specifier:
%c
The sizeof() Operator
How do we know for sure that an int takes 4 bytes and a char takes 1 byte? C provides a built-in tool called the sizeof() operator. It allows you to ask the compiler exactly how much memory a specific data type or variable is using on your specific computer architecture.
Code Examples
#include <stdio.h>
int main() {
// Declaring and initializing different data types
int score = 95;
float price = 19.99;
double exact_weight = 75.123456789;
char rank = 'S';
// Printing the values
printf("Score: %d\n", score);
printf("Price: %f\n", price);
printf("Weight: %lf\n", exact_weight);
printf("Rank: %c\n", rank);
// Checking memory size using sizeof()
printf("\n--- Memory Allocation ---\n");
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu bytes\n", sizeof(char));
return 0;
}
Code Output
Score: 95
Price: 19.990000
Weight: 75.123456789
Rank: S
--- Memory Allocation ---
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 bytes
Code Explanation
First, we created four different boxes (variables) using our four primitive data types. We then printed them using their correct format specifiers (%d, %f, %lf, %c). Notice that the float output automatically added zeroes to make it six decimal places.
In the second part, we used sizeof(). This operator returns an unsigned long integer, which is why we used the format specifier %lu to print the byte size of each data type.
Common Mistakes for Beginners
- Single vs Double Quotes: Writing
char grade = "A";(double quotes) will cause a compiler error. Double quotes are for strings (words). Single quotes'A'are for single characters. - Float Precision Loss: If you try to store 15 decimal places inside a
float, the computer will chop off the extra numbers and give you an inaccurate result. Always usedoublefor high precision. - Integer Overflow: An
intbox has a maximum limit (usually around 2.14 billion). If you try to store 3 billion in it, the box overflows, and your program will output a completely wrong negative number!
Best Practices
- Be memory efficient: Do not use a
doubleif afloatwill do the job perfectly well. Wasting RAM is a sign of poor software engineering. - Avoid magic numbers: When working with data types, always be aware of their maximum limits to prevent application crashes in production.
Interview Questions
Q: What is the difference between float and double?
Answer: Float takes 4 bytes of memory and offers 6-7 decimal places of precision. Double takes 8 bytes of memory and offers 15 decimal places of precision.
Q: What does the sizeof() operator return?
Answer: It returns the size, in bytes, of a variable or a data type as allocated in the memory.
MCQs
Q1. Which data type is best suited to store the number 3.14?
A) int
B) char
C) float (Correct)
D) long
Q2. How many bytes does a char variable typically occupy?
A) 1 byte (Correct)
B) 2 bytes
C) 4 bytes
D) 8 bytes
Practice Questions
- Create a program that stores your age (int), your exact height in meters (float), and your blood group letter (char). Print all three values neatly.
- Use the
sizeof()operator to find out how much memory along doubledata type uses on your machine.
Mini Assignment
Write a "Coffee Shop Receipt" program. You need to declare a character variable for the coffee size ('S', 'M', 'L'), an integer for the number of cups, and a float for the total price. Print out a clean, readable receipt using these variables.
Conclusion
You now know how to tell the computer exactly what shape and size of memory box you need! Understanding data types is crucial because choosing the wrong type can lead to memory waste or catastrophic math errors in your software.
In our next chapter, Chapter 8: Format Specifiers and Escape Sequences, we will dive deeper into how to format our output beautifully. We will learn how to align text, add tabs, and control exactly how many decimal places print on the screen. See you there!
📚 Recommended Reading for Aspiring Developers
Data types are the foundation of everything in computer science. Expand your understanding of how data moves across the tech world with these premium NeoGyan guides:
- Database Data Types: Notice how C has `int` and `char`? Databases use similar concepts to store millions of records. Learn the basics in What is SQL? A Beginner's Guide.
- Transferring Data: When software talks to each other over the internet, how do they share variables? Find out in What is JSON? Explained Simply.
- Connecting Apps: Learn how modern applications pass this data back and forth globally in What is a REST API? Explained Simply.
- Language Comparisons: In C, you must declare data types strictly. Discover languages where you don't have to in Python vs JavaScript: Which Should You Learn?
- Career Upgrade: Want to use your knowledge of data for a high-paying career? Check out this Free IBM Data Analyst Course 2026.