Data types in the C language can be categorized into three types, namely primitive, user-defined, and derived data types. In this article, we shall learn about derived data types.
In C, the data types derived from the primitive or built-in data types are called Derived Data Types. In other words, the derived data types are those data types that are created by combining primitive data types and other derived data types.
There are three derived data types available in C. They are as follows:
- Function
- Array
- Pointer
We shall learn about each of these data types one by one.
1. Functions
A function is called a C language construct which consists of a function-body associated with a function-name. In every program in C language, execution begins from the main function, which gets terminated after completing some operations which may include invoking other functions.
Function Declaration
return_type function_name(data_type param1, data_type param2, ...);
Example
C
// C program to illustrate derived data type - function
#include <stdio.h>
int multiply(int param1, int param2)
{
// return statement which return type int
return (param1 * param2);
}
int main()
{
// declaring parameters to be passed to the function
int param1 = 5, param2 = 3;
// calling the function and storing the return value in
// result
int result = multiply(param1, param2);
// printing the result to the console
printf("%d", result);
return 0;
}
Components of a Function in C
- Return Type: Specifies the type of the value that the function will return after its execution.
- Function Name: It is a unique name that identifies a function. Using this unique name, a function is called from various parts of a program.
- Function Body: A function's body consists of the statements that define what the function actually does. All the operations including the return of the result are done inside the body of a function.
- Parameters: Parameters are the input values passed to the function by the caller. A function can have none or multiple parameters.
- Return Statement: A function returns a value depending on its return type.
For more details about Functions in C, please refer to this article.
2. Arrays
Array in C is a fixed-size collection of similar data items stored in contiguous memory locations. An array is capable of storing the collection of data of primitive, derived, and user-defined data types.
Array Declaration
data_type array_name [size];
Example
C
// C program to illustrate derived data type - Array
#include <stdio.h>
int main()
{
// declaring the size of the array
int N = 3;
// declaring array of type int of size N
int arrI[] = { 18, 36, 54 };
for (int i = 0; i < N; i++)
printf("%d ", arrI[i]);
printf("\n");
// declaring array of type double of size N
double arrD[] = { 3.14, 6.28, 9.42 };
for (int i = 0; i < N; i++)
printf("%lf ", arrD[i]);
return 0;
}
Output18 36 54
3.140000 6.280000 9.420000
Properties of Arrays in C
Following are some defining properties of arrays in C
- Array in C has a fixed size that should be known at compile time.
- An array can only store elements of the same type.
- Elements of the array are stored in the contiguous memory location.
- Array provides random access to its elements.
- An array can have multiple dimensions i.e. directions in which it can grow.
For more details about Arrays in C, please refer to this article.
Pointer
A pointer in C language is a data type that stores the address where data is stored. Pointers store memory addresses of variables, functions, and even other pointers.
Pointer Declaration
data_type * ptr_name;
where,
- data_type: type of data that a pointer is pointing to.
- ptr_name: name of the pointer.
- *: dereferencing operator.
Example:
C
// C program to illustrate derived datatype - pointers
#include <stdio.h>
int main()
{
int var = 20;
// declare pointer variable
int* myPtr;
// note that data type of ptr and var must be same
// assign the address of a variable to a pointer
myPtr = &var;
// printing myPtr to show value stored in myPtr which is
// address of var
printf("Value at myPtr = %p \n", myPtr);
// printing var variable directly
printf("Value at var = %d \n", var);
// using dereferencing operator to get the value myPtr
// is pointing at
printf("Value at *myPtr = %d \n", *myPtr);
return 0;
}
OutputValue at myPtr = 0x7ffd0850df9c
Value at var = 20
Value at *myPtr = 20
Properties of Pointers in C
- A pointer can be of any type such as an integer pointer, function pointer, etc.
- A pointer can be any level deep such as a double pointer(pointer to pointer), triple pointer, etc.
- All types of pointers have the same size on the given platform.
- Pointers may be of different sizes on different platforms.
For more details about Pointers in C, please refer to this article.
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read
C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
7 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
9 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.Need of File Han
13 min read
Operators in C In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.What is an Operator in C?A C operator can be defined as the symbol that helps us
11 min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read