This tutorial explains data types in C programming, which define the type and size of data a variable can store. It covers basic, derived, and user-defined data types with syntax and examples to help beginners understand memory usage and correct data representation in C programs.
1. What Are Data Types in C
Data types specify:
- The type of data a variable can store
- The amount of memory allocated
- The operations that can be performed on the data
2. Classification of Data Types in C
C data types are broadly classified into:
- Basic (Primary) Data Types
- Derived Data Types
- User-defined Data Types
3. Basic (Primary) Data Types
3.1 int
Used to store whole numbers.
int age = 25;
Typical size: 4 bytes
3.2 float
Used to store decimal values with single precision.
float price = 99.75;
Typical size: 4 bytes
3.3 double
Used to store decimal values with double precision.
double distance = 12345.6789;
Typical size: 8 bytes
3.4 char
Used to store a single character.
char grade = 'A';
Typical size: 1 byte
Example Program (Basic Data Types)
#include <stdio.h>
int main() {
int count = 10;
float rate = 5.5;
double total = 1234.5678;
char status = 'Y';
printf("Count: %d\n", count);
printf("Rate: %.2f\n", rate);
printf("Total: %.4lf\n", total);
printf("Status: %c\n", status);
return 0;
}
4. Type Modifiers
Type modifiers change the size or range of basic data types.
Modifiers:
shortlongsignedunsigned
Examples
short int a = 10;
long int b = 100000;
unsigned int c = 50;
5. Derived Data Types
Derived data types are created using basic data types.
5.1 Arrays
int marks[5] = {80, 85, 90, 75, 88};
5.2 Pointers
int x = 10;
int *ptr = &x;
5.3 Functions
int add(int a, int b) {
return a + b;
}
6. User-defined Data Types
6.1 struct
struct Student {
int id;
char name[20];
};
6.2 union
union Data {
int i;
float f;
};
6.3 enum
enum Day {Mon, Tue, Wed, Thu, Fri};
6.4 typedef
typedef unsigned int uint;
uint count = 10;
7. void Data Type
void represents the absence of a value.
Example:
void display() {
printf("Hello");
}
8. Size of Data Types
Use sizeof operator to find memory size.
printf("Size of int: %lu\n", sizeof(int));
Key Points to Remember
- Choose correct data type to optimize memory
- Use
sizeofto verify memory usage - Understand type modifiers for system-level programming