- What are data types in C and why they matter
- What are Primary Data Types (int, char, float, double, void)
- What are Derived Data Types (arrays, functions, pointers)
- What are User-Defined Data Types (structures, unions, enums)
- How to check the size of each data type
What are Data Types in C?
Data types in C specify the type of data a variable can store — whether it's an integer, a floating-point number, a character, or something else. They tell the compiler how much memory to allocate and how to interpret the data.
Think of data types like containers. You wouldn't store cereal in a shoe box, right? Similarly, you need the right "container" for your data. C gives you a variety of containers — choose the one that fits your data best.
💡 Quick Definition: "A data type is a classification of data that tells the compiler how the programmer intends to use the data."
Example: If you want to store employee details like name, salary, and bonus — each requires a different data type:
- Name: "Ajay" → Character/string
- Salary: 80000 → Integer
- Bonus: 0.56 → Float/Double
Types of Data Types in C
Primary Data Types
int, char, float, double, void
Derived Data Types
Arrays, Functions, Pointers
User-Defined Data Types
Structures, Unions, Enums
1. Primary Data Types
1.1 Integer (int)
The int data type is used to store whole numbers (without a fractional part). Examples: 5, 8, 67, 2390.
Integers can be signed (positive and negative) or unsigned (only positive). The memory size depends on the compiler — 2 bytes on 32-bit, 4 bytes on 64-bit.
Syntax: int variable_name;
Example: int rs;
#include <stdio.h>
void main()
{
int i;
i = 20;
printf("\nValue of i = %d", i);
printf("\nSize of i = %u", sizeof(i));
}
Output:
Value of i = 20
Size of i = 4
Integer Data Type Variants:
- short int (short): 1 byte, small range
- int: 2 or 4 bytes, intermediate range
- long int (long): 4 bytes, large range
- long long int (long long): 8 bytes, very large range
1.2 Character (char)
The char data type stores a single character — a letter, digit, or symbol. It uses 1 byte of memory and stores values as ASCII codes.
Syntax: char variable_name = 'c';
Example: char grade = 'A';
int main() {
char ch = 'a';
printf("\nASCII code of %c is %d", ch, ch);
return 0;
}
Output:
ASCII code of a is 97
1.3 Float
The float data type stores real numbers with decimal points. It uses 4 bytes of memory and is a single-precision floating-point type.
int main() {
float y = 427.665;
float x = 1000.5454F;
printf("\nValue of y = %f", y);
printf("\nValue of x = %.2f", y);
}
Output:
Value of y = 427.665000
Value of x = 1000.55
1.4 Double
The double data type stores floating-point numbers with double precision. It uses 8 bytes of memory and can store up to 15-17 decimal digits.
1.5 Void
The void data type has no value and no size. It's used to declare functions that don't return any value.
2. Derived Data Types
2.1 Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations. It's used to handle large amounts of data without declaring many variables.
Syntax: data_type array_name[size];
Example: int numbers[10];
2.2 Functions
A function is a group of statements that perform a specific task. Functions allow code reuse and make programs easier to understand.
- Library functions: Built-in functions like
printf(),scanf(),sqrt() - User-defined functions: Created by the programmer for specific needs
2.3 Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are powerful — they enable dynamic memory allocation and efficient array manipulation.
Syntax: data_type *ptr_var;
Example: int *ptr;
3. User-Defined Data Types
3.1 Structure (struct)
A structure is a user-defined data type that groups variables of different data types into a single unit.
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
int empno;
float salary;
};
int main() {
struct Employee emp1;
strcpy(emp1.name, "Raj");
emp1.empno = 1990;
emp1.salary = 40980.99;
return 0;
}
3.2 Union
A union is similar to a structure, but all members share the same memory location. The memory allocated is equal to the largest member.
float salary;
char name[20];
} e;
int main() {
union Emp e;
e.salary = 20090.034;
strcpy(e.name, "Raj");
return 0;
}
3.3 Enum (enum)
Enum is a user-defined data type used to assign names to integer constants, making code more readable.
int main() {
enum countries cnt;
cnt = America;
printf("%d", cnt);
return 0;
}
Output:
0
Size of Data Types in C
The memory occupied by each data type depends on the compiler architecture (32-bit or 64-bit). Here's a typical output:
#include <limits.h>
int main() {
printf("Size of short int is %2d bytes \n", sizeof(short int));
printf("Size of int is %2d bytes \n", sizeof(int));
printf("Size of long int is %2d bytes \n", sizeof(long int));
printf("Size of float is %2d bytes \n", sizeof(float));
printf("Size of double is %2d bytes \n", sizeof(double));
printf("Size of char is %2d bytes \n", sizeof(char));
return 0;
}
Output:
Size of short int is 2 bytes
Size of int is 4 bytes
Size of long int is 8 bytes
Size of float is 4 bytes
Size of double is 8 bytes
Size of char is 1 bytes
| Data Type | Memory (32-bit) | Memory (64-bit) | Range |
|---|---|---|---|
| short int | 2 bytes | 2 bytes | -32,768 to 32,767 |
| int | 2 bytes | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long int | 4 bytes | 8 bytes | -2,147,483,648 to 2,147,483,647 |
| float | 4 bytes | 4 bytes | 1.2E-38 to 3.4E+38 |
| double | 8 bytes | 8 bytes | 2.3E-308 to 1.7E+308 |
| char | 1 byte | 1 byte | -128 to 127 |
Frequently Asked Questions About Data Types in C
1. What are the three types of data types in C?
Primary/Basic Data Types: int, char, float, double, void
Derived Data Types: arrays, functions, pointers
User-Defined Data Types: structures, unions, enums
2. What is the difference between float and double?
Float is single-precision (4 bytes) with 6-7 decimal digits. Double is double-precision (8 bytes) with 15-17 decimal digits.
3. What is the difference between structure and union?
In a structure, each member has its own memory. In a union, all members share the same memory location.
4. What is the use of void data type?
Void is used to declare functions that don't return any value. It's also used for pointers that can point to any data type.
💡 Quick tip: Choose the smallest data type that can hold your data. It saves memory and improves performance.