Introduction
C is one of the most popular programming languages used in computer programming. It is known for its speed, simple structure, and ability to manage computer memory efficiently. Whether you are a beginner or learning advanced programming concepts, understanding data types is an important part of learning the C language.
A data type in C tells the compiler what kind of value a variable can store. It also determines the type of operations that can be performed on that value. C programming provides different data types, including basic data types, derived data types, and user-defined data types.
Among these, derived data types in C play an important role in organising and handling data. They are created using existing data types and help programmers work with multiple values, memory addresses, and reusable functions. Arrays, pointers, and functions are common examples of derived data types.
Understanding these data types makes it easier to write organised programs, manage memory locations, and work with data structures. In this article, we will explore different C data types, their examples, their uses, and the importance of derived data types in C programming.
What Are Data Types in C?
A data type in C is a classification that tells the compiler what type of value a variable can hold. It helps the compiler understand how to store, process, and use the information in a program.
For example, if you want to store a person’s age, you can use an integer data type. If you want to store a product price, you can use a floating-point data type.
Example:
c
#include
int main() {
int age = 25;
float price = 99.50;
char grade = ‘A’;
printf(“Age: %d\n”, age);
printf(“Price: %f\n”, price);
printf(“Grade: %c\n”, grade);
return 0;
}
Output:
text
Age: 25
Price: 99.500000
Grade: A
In this example:
– int stores whole numbers.
– float stores decimal numbers.
– char stores a single character.
Every variable has a particular data type. The selected type affects its memory requirements and the operations that can be performed on its value.
What Are the Different Types of Data Types in C?
C programming provides different types of data to store and manage information. They are commonly grouped into three main categories: basic data types, derived data types, and user-defined data types.
1. Basic Data Types in C
Basic data types are also known as primary or fundamental data types. They are used to store simple values such as numbers, characters, and decimal values.
The commonly used basic data types include:
Integer (int): Used to store whole numbers, including positive and negative values.
Example:
c
int age = 20;
Character (char): Used to store a single character, such as a letter or symbol.
Example:
c
char grade = ‘A’;
Floating-point (float): Used to store decimal numbers with single precision.
Example:
float price = 45.75;
Double (double): Used to store decimal numbers with greater precision than float.
Example:
c
double salary = 25000.5678;
Void (void): Represents the absence of a value. It is commonly used for functions that do not return a value.
Example:
void display() {
printf(“Welcome to C programming”);
}
These primitive data types are the foundation for creating more complex types in the C programming language.
2. Derived Data Types in C
Derived data types are created using existing data types. They help programmers organise. C is one of the most widely used programming languages for computer programming. This programming language is known for its speed, simplicity, and memory management capabilities. Irrespective of whether you are a beginner or learning advanced programming concepts, understanding data types is essential in learning the C programming language.
Data types in C give information about the value that can be stored in a variable. Data types also indicate the operations that can be done on that value. C programming offers several data types such as basic data types, derived data types, and user-defined data types.
Among these, derived data types in C are important for organizing and managing the data. These are data types that are formed using existing data types and make it easy for programmers to deal with multiple values, memory addresses, and reusable functions. Arrays, pointers, and functions are some of the derived data types in the C programming language.
Understanding these data types makes it easier to write organized programs and handle memory locations and data structures. In this article, we will learn different C data types, their examples, uses, and the significance of derived data types in C programming.
3. User-Defined Data Types in C
User-defined data types in C are created by programmers to organise data according to their requirements. Unlike basic data types, which are already available in C, user-defined data types allow programmers to create custom data structures and represent information more effectively.
The main user-defined data types in C include:
- Structure (struct): Used to group different types of variables under a single name.
- Union (union): Allows different variables to share the same memory location.
- Enumeration (enum): Used to define a set of named integer constants.
- Typedef (typedef): Used to create an alternative name for an existing data type.
These data types are useful when developing programs that handle complex information, such as student records, employee details, banking systems, and inventory management.
What Are the Different Derived Data Types in C?
Derived data types are formed using existing data types. They help programmers handle multiple values, work with memory addresses, and organise program instructions.
C programming mainly includes three commonly discussed derived types: arrays, pointers, and functions. Let’s understand each of them with simple examples.
1. Array in C
An array in C is a collection of elements of the same data type stored under a single variable name. It allows programmers to store and manage multiple values without declaring separate variables for each value.
Each element in an array is identified by an index. Array indexing starts from 0, which means the first element is stored at index 0, the second at index 1, and so on.
Types of Arrays in C
There are three commonly used types of arrays:
1. One-Dimensional Array
A one-dimensional array stores elements in a single sequence or list. It is useful for storing values such as student marks, employee salaries, and product prices.
Syntax:
data_type array_name[size];
Example:
#include <stdio.h>
int main() {
int marks[5] = {80, 75, 90, 85, 95};
printf(“First Mark: %d\n”, marks[0]);
printf(“Second Mark: %d\n”, marks[1]);
return 0;
}
Output:
First Mark: 80
Second Mark: 75
2. Two-Dimensional Array
A two-dimensional array stores elements in rows and columns. It is commonly used to represent tables and matrices.
Syntax:
data_type array_name[rows][columns];
Example:
#include <stdio.h>
int main() {
int numbers[2][2] = {
{10, 20},
{30, 40}
};
printf(“%d\n”, numbers[0][0]);
printf(“%d\n”, numbers[1][1]);
return 0;
}
Output:
10
40
In this example, the array contains two rows and two columns. The expression numbers[0][0] accesses the first element, while numbers[1][1] accesses the last element.
3. Multidimensional Array
A multidimensional array is an array with more than two dimensions. It is useful when working with complex data that needs multiple levels of organisation.
For example, a three-dimensional array can store information using three indexes.
Syntax:
data_type array_name[size1][size2][size3];
Example:
#include <stdio.h>
int main() {
int numbers[2][2][2] = {
{{10, 20}, {30, 40}},
{{50, 60}, {70, 80}}
};
printf(“%d”, numbers[1][0][1]);
return 0;
}
Output:
60
Here, three indexes are used to access an element from the three-dimensional array.
Uses of Arrays in C Programming
- Storing multiple values using a single variable name.
- Managing student marks and employee information.
- Performing sorting and searching operations.
- Working with matrices and mathematical calculations.
- Handling large collections of similar data.
2. Pointer in C
A pointer in C is a variable that stores the memory address of another variable. It allows programmers to access and modify values indirectly through their memory locations.
To understand pointers, you should first understand what a memory address means.
Meaning of Memory Address
Every variable created in a C program is stored somewhere in the computer’s memory. Each memory location has an address that helps the computer identify where the variable is stored.
For example, consider an integer variable:
int number = 25;
The variable number stores the value 25. Its memory address can be accessed using the address-of operator (&).
Pointer Declaration and Syntax
A pointer is declared using the * symbol.
Syntax:
data_type *pointer_name;
For example:
int *ptr;
Here:
- int specifies the type of value the pointer points to.
- * indicates that ptr is a pointer.
- ptr is the pointer variable name.
The two important operators used with pointers are:
- & (Address-of operator): Returns the memory address of a variable.
- * (Dereference operator): Accesses the value stored at the memory address held by a pointer.
Pointer Example in C
#include <stdio.h>
int main() {
int number = 25;
int *ptr = &number;
printf(“Value: %d\n”, number);
printf(“Address: %p\n”, (void *)ptr);
printf(“Value using pointer: %d\n”, *ptr);
return 0;
}
Output:
Value: 25
Address: 0x…
Value using pointer: 25
The memory address will differ depending on the computer and execution environment.
In this example, number stores the value 25, while ptr stores the address of number. Using *ptr, we can access the original value through the pointer.
Uses of Pointers in C
- Accessing and modifying variables through memory addresses.
- Dynamic memory allocation using malloc(), calloc(), and realloc().
- Passing arguments to functions by address.
- Working with arrays and strings.
- Implementing data structures such as linked lists, trees, and graphs.
- Improving flexibility in memory management.
3. Functions in C
A function in C is a block of code designed to perform a particular task. Functions help divide a large program into smaller and manageable sections.
Instead of writing the same code repeatedly, programmers can create a function once and call it whenever required.
In C, functions are commonly discussed as derived types because a function’s type is determined by its return type and parameter types.
Function Declaration and Return Type
A function declaration tells the compiler about the function name, return type, and parameters before the function is used.
A return type specifies the type of value a function sends back after execution.
Some commonly used return types include:
- int: Returns an integer value.
- float: Returns a decimal value.
- char: Returns a character.
- void: Does not return a value.
Function Syntax in C
return_type function_name(parameters) {
// Statements
return value;
}
Function Example in C
#include <stdio.h>
int add(int a, int b);
int main() {
int result = add(10, 20);
printf(“Sum: %d”, result);
return 0;
}
int add(int a, int b) {
return a + b;
}
Output:
Sum: 30
In this example, add() is a function that accepts two integer values and returns their sum. The main() function calls it and displays the result.
Uses of Functions in C Programming
- Reusing code without writing the same statements repeatedly.
- Dividing large programs into smaller sections.
- Improving code readability and maintenance.
- Simplifying debugging and testing.
- Making programs easier to understand and manage.
What Is the Difference Between Basic and Derived Data Types in C?
Basic and derived data types are important parts of the C programming language. Both help programmers store and manage information, but they differ in how they are created and used.
Basic data types are predefined data types provided by C. They are used to store simple values such as integers, characters, and decimal numbers.
Derived data types are formed using existing data types. They help programmers work with collections of values, memory addresses, and functions.
Basic Data Types vs Derived Data Types in C
| Comparison | Basic Data Types | Derived Data Types |
| Meaning | Predefined data types | Types formed using existing types |
| Purpose | Store individual values | Handle collections, addresses, and functions |
| Creation | Already available in C | Created using existing data types |
| Examples | int, char, float, double | Arrays, pointers, functions |
| Memory handling | Stores values directly | Depends on the derived type |
| Usage | Storing numbers and characters | Managing arrays, memory, and program operations |
Key Differences in Memory Handling and Usage
- Basic data types store individual values directly in variables.
- Arrays store multiple elements of the same data type in consecutive memory locations.
- Pointers store memory addresses rather than the actual values they point to.
- Functions organise program instructions into reusable blocks.
- Memory requirements depend on the type, size, and implementation of the variable or object.
For example, an integer variable stores a single number, whereas an integer array can store multiple numbers using one variable name. Similarly, a pointer can store the address of an integer variable.
What Are User-Defined Data Types in C?
User-defined data types allow programmers to create their own data types based on program requirements. They are particularly useful when working with information that contains multiple values or needs a specific structure.
C provides four commonly used user-defined type facilities: structures, unions, enumerations, and typedef.
Structure in C
A structure in C is a user-defined data type that groups different types of variables under a single name. It allows programmers to store related information together, even when the individual values have different data types.
For example, student information may include a name, roll number, and mark. Instead of creating separate variables for every student, a structure can group these details together.
Structure Syntax:
struct structure_name {
data_type member1;
data_type member2;
data_type member3;
};
Structure Example in C:
#include <stdio.h>
struct Student {
int rollno;
char grade;
float mark;
};
int main() {
struct Student s1;
s1.rollno = 101;
s1.grade = ‘A’;
s1.mark = 89.5;
printf(“Roll Number: %d\n”, s1.rollno);
printf(“Grade: %c\n”, s1.grade);
printf(“Mark: %.2f\n”, s1.mark);
return 0;
}
Output:
Roll Number: 101
Grade: A
Mark: 89.50
In this example, the Student structure contains three members: rollno, grade, and mark. The variable s1 is used to access and store the student’s information.
Uses of Structures in C:
- Managing student and employee records.
- Storing customer information.
- Organising related data under one variable name.
- Developing database-related applications.
- Creating complex data structures.
Union in C
A union in C is a user-defined data type that allows multiple members of different data types to share the same memory location.
Unlike a structure, where each member has its own storage, all members of a union share the same memory space. Therefore, a union can store a value in one member at a time without maintaining separate storage for every member.
Union Syntax:
union union_name {
data_type member1;
data_type member2;
data_type member3;
};
Union Example in C:
#include <stdio.h>
union Data {
int number;
float price;
char grade;
};
int main() {
union Data d;
d.number = 100;
printf(“Number: %d\n”, d.number);
d.price = 45.5;
printf(“Price: %.2f\n”, d.price);
d.grade = ‘A’;
printf(“Grade: %c\n”, d.grade);
return 0;
}
Output:
Number: 100
Price: 45.50
Grade: A
In this example, number, price, and grade share the same memory location. Each assignment replaces the previously stored value. Therefore, only the most recently assigned member should be accessed to obtain a meaningful value.
How Do Union Members Share Memory?
The memory allocated for a union is large enough to accommodate its largest member, considering the alignment requirements of the system.
For example, if a union contains an integer, a float, and a character, the total memory requirement is generally based on the largest member and its alignment rather than the sum of all members.
Uses of Unions in C:
- Saving memory when only one member needs to hold a value at a time.
- Working with embedded systems.
- Handling different data formats.
- Developing memory-efficient applications.
Enumeration in C
Enumeration, also known as enum, is a user-defined data type used to create a set of named integer constants.
It makes programs easier to understand because programmers can use meaningful names instead of remembering numerical values.
For example, instead of representing days of the week using numbers, you can create an enumeration containing MONDAY, TUESDAY, and other days.
Enumeration Syntax:
enum enum_name {
value1,
value2,
value3
};
Enumeration Example in C:
#include <stdio.h>
enum Days {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
};
int main() {
enum Days today = WEDNESDAY;
printf(“Day Number: %d”, today);
return 0;
}
Output:
Day Number: 2
In this example, the first enumeration constant starts with the value 0 by default. Therefore, MONDAY is 0, TUESDAY is 1, and WEDNESDAY is 2.
Uses of Enumeration in C:
- Representing days and months.
- Defining application status values.
- Managing menu options.
- Representing different program states.
- Improving code readability.
Typedef in C
typedef is a keyword in C that allows programmers to create an alternative name for an existing data type.
It does not create a completely new data type. Instead, it provides another name that makes declarations easier to read and write.
Typedef Syntax:
typedef existing_data_type new_name;
Typedef Example in C:
#include <stdio.h>
typedef unsigned int Number;
int main() {
Number age = 25;
printf(“Age: %u”, age);
return 0;
}
Output:
Age: 25
In this example, Number is an alternative name for unsigned int. Therefore, the variable age can be declared using Number instead of writing the complete data type.
Uses of Typedef in C:
- Creating shorter names for complex data types.
- Improving code readability.
- Simplifying structure declarations.
- Making programs easier to maintain.
- Reusing type names across different parts of a program.
What Are Type Modifiers in C?
Type modifiers in C are keywords used to change the properties of certain basic data types. They mainly affect the range of values that a variable can store.
For example, you can use type modifiers with integer types to represent a wider range of positive or negative values.
C provides four commonly used type modifiers:
1. Signed
The signed modifier allows an integer variable to store both positive and negative values, including zero.
Example:
signed int number = -100;
Here, the variable number can store negative and positive integer values.
2. Unsigned
The unsigned modifier allows a variable to store only non-negative values. It provides a larger positive range compared to the corresponding signed integer type when both use the same amount of memory.
Example:
unsigned int age = 25;
Here, the variable age can store zero and positive integer values.
3. Short
The short modifier is used to declare a short integer type. It generally requires less memory than a standard integer type, although the exact size depends on the implementation.
Example:
short int number = 100;
4. Long
The long modifier is used to declare an integer type with a potentially larger range than a standard integer type. It can also be used with double to declare long double.
Example:
long int population = 500000;
long long int distance = 9876543210;
Examples of Integer Types and Modifiers
| Data Type | Example | Purpose |
| short int | short int a = 10; | Short integer |
| signed int | signed int a = -20; | Positive and negative integers |
| unsigned int | unsigned int a = 50; | Non-negative integers |
| long int | long int a = 100000; | Long integer |
| long long int | long long int a = 9000000; | Extended integer range |
Type modifiers are useful when programmers need to control the range of values stored in variables and select suitable integer types for their applications.
How Much Memory Do Different Data Types in C Use?
Memory allocation is an important concept in C programming. Every variable requires a certain amount of memory to store its value.
The sizeof operator in C helps programmers find the memory size of a data type or variable in bytes.
The memory size of common C data types can vary depending on the compiler, operating system, and system architecture.
Memory Size of Different Data Types in C
The following table shows common sizes found in many modern systems. These are examples, not fixed values guaranteed by the C language.
| Data Type | Common Memory Size |
| char | 1 byte |
| int | 4 bytes |
| float | 4 bytes |
| double | 8 bytes |
| short int | 2 bytes |
| long int | 4 or 8 bytes |
| long long int | 8 bytes |
1. Integer Types
Integer data types such as short int, int, long int, and long long int are used to store whole numbers.
Their memory requirements depend on the implementation. A common system uses 4 bytes for int and 8 bytes for long long int.
2. Floating-Point Types
The float data type is commonly allocated 4 bytes of memory. It is used to store decimal values with single precision.
3. Character Type
The char data type uses 1 byte of memory in C. It is used to store a single character, such as ‘A’, ‘B’, or ‘C’.
4. Double Data Type
The double data type commonly uses 8 bytes of memory. It provides greater precision than the float data type.
How to Find Memory Size Using the sizeof Operator?
The sizeof operator is used to determine the memory occupied by a data type or variable.
Syntax:
sizeof(data_type);
Example:
#include <stdio.h>
int main() {
printf(“Size of int: %zu bytes\n”, sizeof(int));
printf(“Size of char: %zu bytes\n”, sizeof(char));
printf(“Size of float: %zu bytes\n”, sizeof(float));
printf(“Size of double: %zu bytes\n”, sizeof(double));
return 0;
}
Output on a common system:
Size of int: 4 bytes
Size of char: 1 bytes
Size of float: 4 bytes
Size of double: 8 bytes
The %zu format specifier is used to display the result of the sizeof operator because it returns a value of type size_t.
Important: The memory sizes shown above are common examples. They may differ depending on the compiler and system architecture. Always use sizeof to check the actual memory size in your programming environment.
Why Are Derived Data Types Important in C Programming?
Derived data types play an important role in developing efficient and organised C programs. They allow programmers to work with multiple values, access memory locations, and build complex data structures using existing data types.
Here are some important benefits of derived data types in C:
1. Better Data Organisation
Arrays and other derived types help organise related information. For example, an array can store the marks of multiple students under a single variable name.
2. Efficient Memory Access
Pointers allow programmers to access memory locations directly. They are useful for dynamic memory allocation and working with complex data structures.
3. Code Reusability
Functions allow programmers to write a block of code once and use it multiple times. This reduces code repetition and makes programs easier to maintain.
4. Managing Multiple Data Elements
Arrays help store and process multiple values of the same data type. They are useful when working with large collections of information.
5. Building Data Structures
Pointers and other data types help programmers implement advanced data structures such as linked lists, stacks, queues, trees, and graphs.
6. Improved Program Readability
Using suitable derived data types makes programs more organised and easier to understand. It also helps developers manage complex programming tasks effectively.
Frequently Asked Questions About Derived Data Types in C
1. What are derived data types in C?
Derived data types in C are data types created using existing basic data types. They help programmers manage multiple values, memory addresses, and functions. Arrays, pointers, and functions are common examples of derived data types.
2. What are examples of derived data types in C?
The three commonly discussed derived data types in C are:
- Arrays: Store multiple elements of the same data type.
- Pointers: Store the memory addresses of variables or objects.
- Functions: Represent reusable blocks of program instructions through function definitions and calls.
3. Is an array a derived data type in C?
Yes, an array is a derived data type in C. It stores multiple elements of the same data type under a single variable name. Each element can be accessed using its index, starting from zero.
4. Is a pointer a derived data type in C?
Yes, a pointer is a derived data type in C. It stores the memory address of another variable or object. Pointers are commonly used in dynamic memory allocation, arrays, and data structures.
5. Is a function a derived data type in C?
Yes, function types are classified as derived types in C. A function type is determined by its return type and parameter types. Functions help programmers divide large programs into smaller, reusable sections.
6. What is the difference between primitive and derived data types?
Primitive or basic data types are predefined in C and are used to store simple values such as integers, characters, and decimal numbers.
Derived data types are formed using existing data types. Arrays, pointers, and functions are examples that help programmers manage collections of values, memory addresses, and program operations.
7. Can derived data types be nested in C?
Yes, derived data types can be combined to create more complex types in C.
For example, programmers can create an array of pointers, a pointer to an array, or a pointer to a function.
Example:
int *ptr[5];
Here, ptr is an array of five integer pointers. Each element can store the address of an integer variable.
Combining derived types helps programmers develop advanced applications and manage complex data structures.
8. What is the difference between C and C++ derived data types?
C and C++ both support derived data types such as arrays, pointers, and functions.
However, C++ provides additional language features, including references, that are not available as a standard language feature in C. Both languages also support user-defined types, but C++ introduces additional capabilities such as classes, inheritance, and object-oriented programming.
Therefore, C++ provides more advanced type-related features compared to C.
Conclusion
Derived data types are an important concept in C programming. They help programmers organise information, manage memory, and develop efficient programs using existing basic data types.
Arrays allow programmers to store multiple values, pointers provide access to memory addresses, and functions make code reusable and organised. Along with these, user-defined data types such as structures, unions, and enumerations help manage complex information.
Understanding basic, derived, and user-defined data types is essential for anyone learning C programming. Beginners should practise these concepts with simple programs to understand how they work in real-world applications.
If you want to build a strong foundation in programming, start practising C programs regularly and gradually move towards advanced concepts such as pointers, memory management, and data structures.
Learn programming through practical examples, build your coding skills, and take the next step towards becoming a software developer!
