Derived Data Types in C
Last Updated :
06 Sep, 2023
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
#include <stdio.h>
int multiply( int param1, int param2)
{
return (param1 * param2);
}
int main()
{
int param1 = 5, param2 = 3;
int result = multiply(param1, param2);
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
#include <stdio.h>
int main()
{
int N = 3;
int arrI[] = { 18, 36, 54 };
for ( int i = 0; i < N; i++)
printf ( "%d " , arrI[i]);
printf ( "\n" );
double arrD[] = { 3.14, 6.28, 9.42 };
for ( int i = 0; i < N; i++)
printf ( "%lf " , arrD[i]);
return 0;
}
|
Output
18 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
#include <stdio.h>
int main()
{
int var = 20;
int * myPtr;
myPtr = &var;
printf ( "Value at myPtr = %p \n" , myPtr);
printf ( "Value at var = %d \n" , var);
printf ( "Value at *myPtr = %d \n" , *myPtr);
return 0;
}
|
Output
Value 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.
Share your thoughts in the comments
Please Login to comment...