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.
Similar Reads
Derived Data Types in C++
The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. They are generally the data types that are created from the primitive data types and provide some additional functionality. In C++, there are four different derived data types: Table of Co
4 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: [GFGTABS] C++ int number; [/GFGTABS]The above statement declares a variable with name number that can store integer values. C is a static
6 min read
What is data type of FILE in C ?
Prerequisite : Basics of File Handling In C language, while file handling is done a word FILE is used. What is FILE? Example FILE *fp1, *fp2; While doing file handling we often use FILE for declaring the pointer in order to point to the file we want to read from or to write on. As we are declaring t
3 min read
Data Type Modifiers in C
In C, data type modifiers are the keywords used to modify the original sign or length/range of values that various primitive data types hold such as int, char, and double. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { // Using unsigned int and trying // to store
4 min read
Type Conversion in C
In C, type conversion refers to the process of converting one data type to another. It can be done automatically by the compiler or manually by the programmer. The type conversion is only performed to those data types where conversion is possible. Let's take a look at an example: [GFGTABS] C #includ
4 min read
C Compound Data Type Quizzes
In C, compound data types are those data types that are derived from basic data types. They provide an interface to use the basic data types is different ways to satisfy our requirement. They are frequently used to handle real world cases it is a must for programmers to have good clear understanding
2 min read
Types of User Defined Functions in C
A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
4 min read
Catch block and type conversion in C++
Predict the output of following C++ program. C/C++ Code #include <iostream> using namespace std; int main() { try { throw 'x'; } catch(int x) { cout << " Caught int " << x; } catch(...) { cout << "Default catch block"; } } Output: Default catch block In th
2 min read
What is the size_t data type in C?
size_t is an unsigned integer data type that is defined in various header files such as: <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h>It's a type which is used to represent the size of objects in bytes and is therefore used as the return type
4 min read
Casting Operators in C++
The casting operators is the modern C++ solution for converting one type of data safely to another type. This process is called typecasting where the type of the data is changed to another type either implicitly (by the compiler) or explicitly (by the programmer). Let's take a look at an example: [G
5 min read