Data Types in C
Data Types in C
Introduction
In C programming, data types are one of the most important concepts. They define what kind of data a variable can store, how much memory it occupies, and what operations can be performed on it.
Understanding data types is essential for writing efficient, accurate, and error-free programs.
What are Data Types in C?
A data type specifies:
- The type of data (integer, character, decimal, etc.)
- The memory size required
- The range of values that can be stored
👉 In simple terms:
Data types tell the compiler what kind of data you are working with.
Categories of Data Types in C
C data types are broadly classified into:
- Basic (Primary) Data Types
- Type Modifiers
- Derived Data Types
- User-defined Data Types
1. Basic (Primary) Data Types
These are the fundamental data types:
| Data Type | Description | Example | Size (Approx) |
|---|---|---|---|
int |
Integer numbers | 10, -5 | 4 bytes |
float |
Decimal numbers | 3.14 | 4 bytes |
char |
Single character | 'A' | 1 byte |
double |
High precision decimal | 3.14159 | 8 bytes |
void |
No value | — | 0 byte |
2. Type Modifiers
Modifiers change the size or range of basic data types:
| Modifier | Description | Example |
|---|---|---|
short |
Smaller integer | short int a; |
long |
Larger integer | long int b; |
long long |
Very large integer | long long int c; |
signed |
Positive & negative values | signed int d; |
unsigned |
Only positive values | unsigned int e; |
Example Using Modifiers
#include <stdio.h>
int main() {
short int a = 10;
long int b = 100000;
unsigned int c = 50;
printf("Short = %d\n", a);
printf("Long = %ld\n", b);
printf("Unsigned = %u\n", c);
return 0;
}
3. Derived Data Types
These are created from basic data types:
| Type | Description |
|---|---|
| Arrays | Collection of elements |
| Pointers | Store memory address |
| Functions | Block of reusable code |
Example:
int arr[5]; // Array
int *ptr; // Pointer
4. User-Defined Data Types
These are created by the programmer:
| Type | Description |
|---|---|
struct |
Group of different data types |
union |
Shares memory among members |
enum |
Named integer constants |
typedef |
Create new name for data type |
Example:
struct Student {
int id;
char name[20];
};
Complete Summary Table
| Category | Types Included |
|---|---|
| Basic | int, float, char, double, void |
| Modifiers | short, long, signed, unsigned |
| Derived | array, pointer, function |
| User-defined | struct, union, enum, typedef |
Example Program Using Data Types
#include <stdio.h>
int main() {
int age = 18;
float marks = 85.5;
char grade = 'A';
double pi = 3.14159;
printf("Age = %d\n", age);
printf("Marks = %f\n", marks);
printf("Grade = %c\n", grade);
printf("Pi = %lf\n", pi);
return 0;
}
Output
Age = 18
Marks = 85.500000
Grade = A
Pi = 3.141590
Format Specifiers
| Data Type | Format Specifier |
|---|---|
int |
%d |
float |
%f |
char |
%c |
double |
%lf |
unsigned int |
%u |
long int |
%ld |
Common Mistakes
- ❌ Using wrong data type
- ❌ Incorrect format specifier
- ❌ Not initializing variables
- ❌ Confusing float and double
Pro Tips
- ✔ Use appropriate data types for memory efficiency
-
✔ Use
doublefor high precision calculations - ✔ Always initialize variables
- ✔ Practice writing programs using different data types
Conclusion
Data types are the foundation of C programming. They help you store and manipulate data effectively. Mastering data types will make your programming journey much easier and more efficient.
👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.
Comments
Post a Comment