LearnContact
Lesson 118 min read

Data Types

Every variable stores a specific type of data. In this lesson, you will learn what data types are, how memory is allocated, and how to choose the correct data type for your variables.

Introduction

Imagine you are filling different containers in your kitchen. Water is stored in a bottle, rice in a large container, and sugar in a small jar. Each container is designed to store a specific type of item. Similarly, in C programming, every variable stores a specific type of data. The compiler needs to know what kind of data a variable will store before allocating memory for it. This information is provided using Data Types.

What is a Data Type?

A Data Type specifies the type of value a variable can store. It tells the compiler:

  • What kind of data will be stored.
  • How much memory should be allocated.
  • What operations can be performed on the data.

Without data types, the compiler would not know how to handle the stored values.

Why Do We Need Data Types?

Data types help the compiler allocate appropriate memory, store data efficiently, perform valid operations, detect programming errors, and improve program performance. Every variable in C must have a data type.

Real-World Analogy

Imagine a supermarket. Different products are stored in different sections like Vegetables, Fruits, Dairy, Electronics, and Clothing. Each section stores a different type of item. Similarly, memory stores different kinds of data using different data types.

In a Supermarket

  • Vegetables Section
  • Fruits Section
  • Dairy Section
  • Electronics Section

In Computer Memory

  • int (Whole numbers)
  • char (Characters)
  • float (Decimals)
  • double (High precision)

How Data Types Work

When you declare a variable, the compiler allocates memory based on its data type.

Variable Declaration
Data Type Identified
Memory Allocated
Value Stored

Classification of Data Types

C data types are generally classified into four main categories:

Basic (Primary)

Fundamental types provided by C.

  • int, char
  • float, double

Derived

Created from basic data types.

  • Arrays
  • Pointers
  • Functions

User-Defined

Created by the programmer.

  • struct
  • union
  • enum, typedef

Void

Represents no value.

  • Used in functions
  • No parameters/return

Basic Data Types

Basic or Primary data types are the fundamental data types provided by C. They are used to store simple values and are the most commonly used data types in C programs.

Derived & User-Defined Data Types

Derived Data Types

Created from the basic data types. Examples include Arrays, Functions, and Pointers. These will be discussed in later lessons.

User-Defined Data Types

Programmers can create their own data types to organize complex data. Examples include Structure (struct), Union (union), Enumeration (enum), and typedef.

Memory Allocation

Different data types require different amounts of memory. The exact size may vary depending on the compiler and operating system, but typical sizes are:

Data TypeTypical Size
char1 Byte
int4 Bytes
float4 Bytes
double8 Bytes

Choosing the Correct Data Type

Always choose a data type based on the information you want to store. Choosing the appropriate data type improves memory usage and program efficiency.

InformationSuitable Data Type
Student Ageint
Student Name Initialchar
Product Pricefloat
Scientific Valuedouble

Real-World Examples

Suppose you are developing a Student Management System. The application stores different pieces of information, each requiring a different data type:

InformationData Type
Roll Numberint
Student Gradechar
Percentagefloat
CGPAdouble

Data Type vs Variable

Many beginners confuse these two concepts. Here is the difference:

FeatureData TypeVariable
PurposeDefines the type of dataStores the actual value
MemoryDetermines memory allocationOccupies memory
UsageSpecified before variable nameCreated using an identifier
Example
int age;

// int  → Data Type
// age  → Variable

Why Different Data Types Exist

Imagine storing a student's age. Possible values are 18, 20, and 25. Using an int is sufficient. Now consider storing a percentage like 95.75. An integer cannot store decimal values, so a floating-point data type is required. Different kinds of data require different data types.

Advantages of Data Types

Efficient Memory

Allocates only the required amount of memory.

Faster Execution

Optimized operations based on data size.

Better Error Detection

Compiler catches invalid operations.

Improved Readability

Code becomes self-explanatory.

Common Beginner Mistakes

Using the Wrong Data Type
// Incorrect choice:
int price; // Price may contain decimals

// Better choice:
float price;
Storing Characters in Integer Variables
// Incorrect:
int grade;

// Better:
char grade;
Confusing float and double

Both store decimal values. Use float for general decimal numbers and double when higher precision is required.

Choosing Large Data Types Unnecessarily

Avoid using larger data types like double when a smaller one such as int or float is sufficient. This helps optimize memory usage.

Best Practices

  • Choose the correct data type for each variable.
  • Use int for whole numbers.
  • Use char for single characters.
  • Use float for decimal values.
  • Use double when higher precision is required.
  • Use meaningful variable names along with appropriate data types.

Frequently Asked Questions

What is a data type?

A data type specifies the kind of data a variable can store.

Why are data types important?

They help the compiler allocate memory correctly and perform appropriate operations.

Can two variables have different data types?

Yes. Different variables can store different kinds of data.

Which data type stores decimal values?

Both float and double store decimal values.

Which data type stores a single character?

The char data type stores a single character.

Key Takeaways

  • Every variable must have a data type.
  • Data types determine memory allocation.
  • C provides basic, derived, and user-defined data types.
  • Common basic data types are int, char, float, double, and void.
  • Choosing the correct data type improves program efficiency and readability.

Summary

Data types are one of the most fundamental concepts in C programming. They define the kind of data that variables can store and determine how much memory is allocated. By selecting appropriate data types, programmers can write efficient, organized, and reliable programs. Understanding data types is essential because almost every variable, function, and data structure in C depends on them.

Next Lesson →

Type Conversion