Data Types in C
A variable stores data, but what kind of data can it store?
Can it store numbers?
Can it store decimal values?
Can it store characters?
The answer depends on its data type.
A data type tells the compiler what type of value a variable can hold, how much memory it requires, and what operations can be performed on it.
🎯 Learning Objectives
After completing this lesson, you will be able to:
- Understand what data types are.
- Learn the built-in data types in C.
- Understand memory allocation.
- Learn data type modifiers.
- Use format specifiers correctly.
- Write programs using different data types.
📖 What is a Data Type?
A Data Type specifies the type of data that a variable can store.
For example,
- Age → Integer
- Salary → Decimal
- Grade → Character
Every variable must have a data type.
Example
cint age; float salary; char grade;
Why Are Data Types Important?
Data types help the compiler:
- Allocate memory.
- Perform calculations correctly.
- Prevent invalid operations.
- Improve program performance.
Categories of Data Types
C provides four major categories.
textData Types │ ├── Basic Data Types │ ├── int │ ├── char │ ├── float │ └── double │ ├── Derived Data Types │ ├── Arrays │ ├── Pointers │ └── Functions │ ├── Enumeration │ └── Void
For now, we will focus on the basic data types.
Integer (int)
The int data type stores whole numbers.
Examples
- 10
- 25
- -40
- 1000
Example
c#include <stdio.h> int main() { int age = 20; printf("%d", age); return 0; }
Output
text20
Character (char)
The char data type stores a single character.
Example
c#include <stdio.h> int main() { char grade = 'A'; printf("%c", grade); return 0; }
Output
textA
Float
The float data type stores decimal numbers.
Example
c#include <stdio.h> int main() { float cgpa = 8.75; printf("%.2f", cgpa); return 0; }
Output
text8.75
Double
The double data type stores large decimal numbers with greater precision.
Example
c#include <stdio.h> int main() { double pi = 3.1415926535; printf("%.10lf", pi); return 0; }
Output
text3.1415926535
Memory Size
| Data Type | Typical Size |
|---|---|
| char | 1 Byte |
| int | 4 Bytes |
| float | 4 Bytes |
| double | 8 Bytes |
Checking Size Using sizeof()
c#include <stdio.h> int main() { printf("Size of int = %zu\n", sizeof(int)); printf("Size of char = %zu\n", sizeof(char)); printf("Size of float = %zu\n", sizeof(float)); printf("Size of double = %zu\n", sizeof(double)); return 0; }
Format Specifiers
| Data Type | Format Specifier |
|---|---|
| int | %d |
| char | %c |
| float | %f |
| double | %lf |
Example
cprintf("%d", age); printf("%c", grade); printf("%f", salary); printf("%lf", pi);
Data Type Modifiers
Modifiers change the size or range of a data type.
Common modifiers are:
- short
- long
- signed
- unsigned
Example
cshort int a; long int b; unsigned int c; long long int d;
Real-Life Example
c#include <stdio.h> int main() { char platform[] = "KauraX"; int students = 500; float rating = 4.9; printf("Platform : %s\n", platform); printf("Students : %d\n", students); printf("Rating : %.1f", rating); return 0; }
Output
textPlatform : KauraX Students : 500 Rating : 4.9
Choosing the Correct Data Type
| Requirement | Data Type |
|---|---|
| Age | int |
| Marks | int |
| Price | float |
| PI | double |
| Grade | char |
Common Beginner Mistakes
❌ Using %d for float
cfloat x = 10.5; printf("%d", x);
Correct
cprintf("%f", x);
❌ Using double with %f
Correct
cprintf("%lf", value);
❌ Writing characters using double quotes
Wrong
cchar grade = "A";
Correct
cchar grade = 'A';
Best Practices
- Use
intfor whole numbers. - Use
floatfor decimal values. - Use
doublewhen higher precision is required. - Use
charfor single characters. - Use meaningful variable names.
🧠 Interview Questions
- What is a data type?
- Why are data types necessary?
- Differentiate between float and double.
- What is the purpose of
sizeof()? - What are data type modifiers?
- Explain format specifiers.
✍ Practice Questions
- Declare variables of different data types.
- Print the size of each data type.
- Display your name, age, CGPA, and grade.
- Write a program using
sizeof(). - Differentiate between
floatanddouble.
📚 Lesson Summary
In this lesson, you learned:
- What data types are.
- Basic data types in C.
- Memory size of each data type.
- Format specifiers.
- Data type modifiers.
- Using
sizeof(). - Common mistakes and best practices.
Understanding data types helps you write efficient and memory-friendly programs.
🚀 What's Next?
In the next lesson, Constants and Literals, you will learn:
- What constants are.
- Different types of literals.
- The
constkeyword. #defineconstants.- Symbolic constants.
- Practical examples.
Happy Coding with KauraX 💙