Data Types in C: Basic, Derived and User-Defined Types
Data types in C define the type of data a variable can store and the amount of memory allocated to it. Understanding data types in C is one of the most important fundamentals for learning the C programming language, as it directly affects program efficiency, memory usage, and correctness.
C is a statically typed language, which means every variable must be declared with a specific data type before it is used. This blog explains all major data types in C with clear classification, examples, and practical explanations for beginners.
What Are Data Types in C?
In C programming, a data type specifies:
The type of value a variable can store
The size of memory allocated
The range of values allowed
The operations that can be performed on the data
Proper use of data types helps in writing efficient, optimized, and error-free programs.
C data types are broadly classified into four categories:
Basic (Primitive) Data Types
Basic data types are the fundamental building blocks of C programming.
The int data type is used to store whole numbers without decimal points.
Size: 2 or 4 bytes (depends on compiler)
Range: -32,768 to 32,767 (for 2 bytes)
The float data type stores decimal or fractional values.
Precision: Up to 6 decimal places
The double data type is used for storing large decimal values with higher precision.
double distance = 12345.6789;
Precision: Up to 15 decimal places
The char data type stores a single character.
Range: -128 to 127 or 0 to 255
Derived data types are created using basic data types.
An array stores multiple values of the same data type in a contiguous memory location.
int marks[5] = {80, 85, 90, 75, 88};
Pointers store the memory address of another variable.
Pointers are widely used for dynamic memory allocation and efficient data handling.
A structure allows grouping different data types under one name.
Structures are commonly used to represent real-world entities.
A union is similar to a structure, but all members share the same memory location.
Only one member can hold a value at a time.
User-Defined Data Types in C
User-defined data types allow programmers to define their own data types.
typedef is used to create an alias for an existing data type.
typedef unsigned int uint;
An enumeration defines a set of named integer constants.
enum Days {Mon, Tue, Wed, Thu, Fri};
Enums improve code readability and maintainability.
The void data type represents the absence of a value.
Functions that return no value
Generic pointers (void *)
The size of data types may vary depending on the compiler and system architecture.
Use the sizeof() operator to check size dynamically.
Why Data Types Are Important in C
Efficient memory utilization
Improved program reliability
Clear data representation
Choosing the correct data type is critical for system-level and embedded programming.
Frequently Asked Questions (FAQs)
What is the most commonly used for numeric values.
What is the difference between float and double?
double provides higher precision and uses more memory than float.
What is a derived data type in C?
Derived data types are created from basic data types, such as arrays, pointers, and structures.
What is void data type used for?
It is used for functions that return no value and generic pointers.
Can data type size vary in C?
Yes, data type size depends on the compiler and system architecture.
Understanding data types in C is essential for writing efficient and reliable programs. From basic types like int and char to complex user-defined structures, data types form the foundation of C programming.
A strong grasp of data types helps developers manage memory effectively and build scalable applications.