Table of Contents
What Are Data Types in C?
In C every variable must have a data type. It defines:
What type of data will the variable store
How much memory will it use
What operations can be performed on it
Every variable in C must be declared with a data type before it is used. This allows the compiler to allocate the correct amount of memory and apply appropriate operations.
C data types are mainly classified into three types:
Primitive (Basic) Data Types
Derived Data Types
User-Defined Data Types
Let us look at each in detail.

1. Basic Data Types in C
These are the most commonly used data types in day-to-day programming. Basic data types are the fundamental building blocks of C programming. They are predefined and store simple values.
a. int (Integer Type)
Used to store whole numbers without decimal point (positive or negative). It is represented as %d. They are mainly stored in binary form(0s and 1s). It is mainly used for storing non-fractional data such as a person’s age the quantity of items etc.
if int x = 10 then it is stored as
- 00000000 00000000 00000000 00001010
int marks = 85; long int population = 7800000000;
| Type | Size | Range |
|---|---|---|
| int | 4 bytes | –2,147,483,648 to 2,147,483,647 |
| short int | 2 bytes | –32,768 to 32,767 |
| long int | 8 bytes | ±9 quintillion approx |
b. float – Single Precision Decimal Number
Stores decimal values with 6-7 digits of precision. Float has a size of 4 bytes. It is stored using the IEEE 754 floating point format which splits the memory into
- Sign bit
- Exponent
- fraction
It is represented as %f. They are used in calculators where precision is important and also in other areas like temperature measurement.
float temperature = 36.5;
c. double – Double Precision Decimal Number
Provides higher precision: 15-16 digits. Double has a size of 8 bytes. It is represented as %lf. It is used where accuracy is critical such as scientific simulations, storing the value of mathematical constraints(pi) etc.
double pi = 3.141592653589793;
d. char – Character Type
Stores a single character like letter, digit or symbol. It has a size of 1 byte. It is represented as %c. It is used for processing text input string operations etc. It stores the ASCII value of a character.
A has an ASCII value of 65. So 65 is stored in binary format 01000001
char grade = 'A';
e. void – Empty Data Type
It indicates “no value.” Used mainly in functions. It is also used for generic pointers in memory operations. Used to specify that a function returns no value (eg. void main() or to declare a generic pointer that can point to any type of data (void*).
void display() {
printf("Welcome to Qubrica");
}
#include
int age = 20; // int
long long int population = 7800000000LL; // long int
float price = 99.99f; // float
double pi = 3.141592653589793; // double
char grade = 'A'; // char
void display() {
printf("Hello!\n");
}
int main() {
printf("Age: %d\n", age);
printf("Population: %lld\n", population);
printf("Price: %.2f\n", price);
printf("Pi : %.15lf\n", pi);
printf("Grade: %c\n", grade);
display();
return 0;
}
2. Derived Data Types in C
Derived data types are created using basic data types. They allow programmers to build more complex data structures.
a. Arrays
A collection of elements of the same data type stored in consecutive memory locations. Arrays store elements in continuous memory locations.
int numbers[3] = {10, 20, 30};in int numbers[3] = {10, 20, 30} the numbers 10, 20 and 30 can be stored in the memory addresses 1000, 1004 and 1008 respectively.
b. Pointers
They are variables that store the address of another variable. Its size depends on the system( 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.
int x = 10; int *p = &x;
When int x = 10; is executed, the integer variable x stores the value 10 at a memory location, for example address 2000. When int *p = &x; is executed, the pointer p stores the memory address of x, that is 2000, rather than the value itself.
c. Functions
A function is a modular code block that executes a specific task and can be called repeatedly. Functions also return data types.
int add(int a, int b) {
return a + b;
}Consider an example code below
#include
// Function to display array elements using pointer
void display(int *arr, int size) {
for(int i = 0; i < size; i++) {
printf("%d ", *(arr + i)); // pointer accessing array
}
}
int main() {
int a[5] = {10, 20, 30, 40, 50}; // array
display(a, 5); // function call (array as pointer)
return 0;
}
// 10 20 30 40 50
3. User-Defined Data Types
User-defined data types allow programmers to define their own data structures according to program requirements.
a. struct
They are used to group different types of variables. It stores each member separately in sequential memory. The size of a struct is equal to the sum of all members in it.
struct Student {
char name[20];
int age;
float marks;
};b. union
A special data type where multiple variables share the same memory location. The size of union is equal to the size of the largest member.
union Data {
int i;
float f;
};c. enum
A user-defined type that assigns names to integer constants for readability.
enum Week { MON, TUE, WED, THU, FRI, SAT, SUN };Consider the code below
// Structure
struct Student {
char name[20];
int age;
float marks;
};
// Union
union Data {
int i;
float f;
};
// enum
enum Week { MON, TUE, WED, THU, FRI, SAT, SUN };
Type Casting
Type casting is the process of converting a value from one data type to another so that it can be correctly stored, processed or interpreted by the program. In low-level languages like C and C++ type casting is very important because the programmer has direct control over memory and data representation.
Why Type Casting Is Needed
• Different data types are used in the same expression
• Precision must be preserved or intentionally reduced
• Hardware-level operations need exact data sizes
• Avoiding logical errors such as integer division
There are two types of Type Casting
1. Implicit Type Casting (Automatic Conversion): Implicit type casting is a conversion that is automatically performed by the compiler when a smaller data type is converted into a larger or compatible data type.
The compiler follows type promotion rules:
• char → int
• int → float
• float → double
This is done to prevent data loss.
2. Explicit Type Casting (Manual Conversion): Explicit type casting is a conversion that the programmer manually specifies to change a value from one data type to another.
Syntax:
(type) expression
Consider the example program below to demonstrate implicit and explicit type casting.
#include
int main() {
/* Implicit Type Casting */
int num = 25;
float avg;
avg = num; // int -> float (implicit casting)
// Explicit Type Casting
int a = 7, b = 2;
float division;
division = (float)a / b; // explicit casting to avoid integer division
printf("Implicit Type Casting:\n");
printf("Integer = %d\n", num);
printf("Float after conversion = %.2f\n\n", avg);
printf("Explicit Type Casting:\n");
printf("Division result = %.2f\n", division);
return 0;
}
// Implicit Type Casting:
// Integer = 25
// Float after conversion = 25.00
// Explicit Type Casting:
// Division result = 3.50
Importance of Data Types in Programming
1. Memory Allocation Size:
Each data type occupies a fixed amount of memory, so choosing the correct type prevents memory wastage and avoids overflow errors.
2. Memory Layout & Alignment:
Proper data types ensure correct memory alignment, reducing padding and enabling faster access to data structures.
3. Performance:
Smaller data types improve performance by fitting better in CPU caches and reducing memory bandwidth usage.
4. Direct Hardware Interaction:
Correct data types allow accurate bitwise operations and proper reading or writing of hardware registers.
5. Safety & Predictability:
Using appropriate data types prevents buffer overflows, data corruption, and undefined program behavior.
Frequently Asked Questions (FAQs)
1) What are static data types in C?
Static data types are the built-in (fixed) data types whose size and type are known at compile time, such as int, float, char and double.
2) How many data types are there in C?
C has three categories of data types:
Primitive (Basic) data type, Derived data type, User-defined data type. Each category contains multiple types.
3) What is a user-defined data type?
A user-defined data type is a type created by the programmer using keywords like struct, union, enum and typedef.
4) What is a derived data type in C?
Derived data types are constructed by using the basic (or primary) data types. Examples include arrays, pointers, functions and structures.
5) What is boolean data type in C?
C uses _Bool (or bool from <stdbool.h>) to represent boolean values: true and false.
6) What are primitive and non-primitive data types in C?
Primitive (basic): Predefined types like int, char, float, and double.
Non-primitive: More complex types such as arrays, structures, unions, pointers, enumerations, and functions.