What are Data Types in C?
In C, data types define the type of data that a variable can store. They help the compiler understand how much memory is needed and what type of operations can be performed on the data.
For example, integers are used for whole numbers, characters are used for single letters, and floating-point types are used for decimal values. Choosing the correct data type helps in efficient memory usage and avoids errors.
Example:
int age = 20; // Integer
float height = 5.7; // Decimal value
double salary = 45678.98; // Double precision decimal
char grade = 'A'; // Character
Why Use Data Types?
- To efficiently manage memory.
- To store different kinds of values correctly.
- To perform valid operations on data.
- To improve program accuracy and performance.
Types of Data Types in C
Data types in C are mainly divided into the following categories:
- Basic (Primary) Data Types
- Derived Data Types
- User-defined Data Types
- Void Data Type
1. Basic (Primary) Data Types
These are the fundamental data types used in C programming. They store simple values such as integers, decimal numbers, and characters.
Basic Data Types in C:
- int – stores whole numbers.
- float – stores decimal numbers (single precision).
- double – stores decimal numbers with higher precision.
- char – stores a single character.
Memory Size and Range of Basic Data Types
| Data Type | Size (in Bytes) | Range (Approximate) | Example |
|---|---|---|---|
| int | 4 | -2,147,483,648 to 2,147,483,647 | int age = 20; |
| float | 4 | ±3.4e−38 to ±3.4e+38 | float marks = 89.5; |
| double | 8 | ±1.7e−308 to ±1.7e+308 | double salary = 45000.75; |
| char | 1 | -128 to 127 | char grade = 'A'; |
Example of Basic Data Types
#include <stdio.h>
int main() {
int rollNo = 101;
float percentage = 92.5;
double bankBalance = 123456.789;
char grade = 'A';
printf("Roll No: %d\n", rollNo);
printf("Percentage: %.1f%%\n", percentage);
printf("Bank Balance: %.3lf\n", bankBalance);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Roll No: 101
Percentage: 92.5%
Bank Balance: 123456.789
Grade: A
Explanation:
intstores whole numbers such as roll number.floatstores decimal values like percentage.doublestores large decimal numbers with more precision.charstores a single character.%dis the format specifier used for integers.%fis used for float values.%lfis used for double values.%cis used for characters.
2. Derived Data Types
Derived data types are created using basic data types.
- Arrays – collection of similar data elements.
- Pointers – store memory addresses.
- Functions – blocks of reusable code.
Example:
int numbers[5] = {1, 2, 3, 4, 5}; // Array
int *ptr = &numbers[0]; // Pointer
3. User-defined Data Types
These data types are created by programmers according to program requirements.
- struct – groups different types of data together.
- union – shares memory among members.
- enum – stores named integer constants.
- typedef – creates a new name for an existing data type.
Example:
struct Student {
int rollNo;
char name[50];
float marks;
};
enum Day {MON, TUE, WED};
typedef unsigned int uint;
4. Void Data Type
The void data type represents “no value”. It is mainly used in functions that do not return anything.
void display() {
printf("Hello Students");
}
Summary:
- Data types define what kind of value a variable can store.
- Basic data types include int, float, double, and char.
- Derived data types are created from basic types.
- User-defined data types are created by programmers.
- Void represents no value.
- Correct data type selection improves memory efficiency and program accuracy.