×

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be stored may be an integer, a float, a string, etc.

There are various data types provided by the ANSI C. The Data types are divided into three classes:

  1. Basic or Primary data types
  2. Derived data types
  3. User defined data types

Data Types

 

Basic Data Types

 

Integer

 

↓

 

int

Real

 

↓

 

float

double

void

char

 

 

Derived Data Types

 

 

Array

 

Pointer

 

Function

User Defined Data Types

 

 

Structure

 

Union

 

Enum

 

*  Each data type mentioned requires a different amount of memory and they vary depending on the type.

 

Derived Data Type

Derived Data types can be defined as the type of data types that are derived from fundamental data types.

They are derived from the primitive data types by inclusion of some elements of the basic or primary data types. They are used in order to represent single/multiple values.

The derived data types have advanced properties and are used to perform complex tasks in C programming. The derived data types don’t create new data types, they only add some functionality to existing data types.

Types of Derived Data types

The C language supports three fundamental derived data types. They are as follows:

  1. Arrays
  2. Pointers
  3. Functions

Arrays

An array is a collection of items or logically related variables of identical data type which are stored in contiguous memory locations. Once the size of array is declared, it cannot be changed.

The items or elements in the array are stored in contiguous memory locations, i.e., one after another.

Program 1: Printing the elements of an arrays

#include<stdio.h>


int main()
{


int arr[10]= {1,7,10,21,3,9,125,729,800,369} ;  //declaring the elements of array
int i; 						        //declaring the local variable
printf("The Elements of array are \n");


for(i=0; i<10 ;i++)
{
printf("%d\n",arr[i]);
}
return  0;
}

Output of the program:

Derived Data Types in C

Pointers

A pointer is a variable and this variable contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization of pointers is to access the memory and also, to govern the address (manipulation of address).

For eg, If a is a variable of type int having value \0 stored at address 3000 and address of a is assigned to some other variable b, then b is a pointer variable pointing to memory address stored in it. In simple words, b is a pointer variable containing the address of a (i.e., 3000).

Program 2: Retrieving the value stored and address of variable using pointers

#include <stdio.h>
#include <conio.h>
 
int main () 
{
   int  count = 5;    //declaring the local variables
   int *ptr = &count;  //pointer variable
 
   printf("The value of count variable is %d\n", count);


   printf("Address of count variable: %x\n", ptr);


   printf("Value retrieved through pointer : %d\n", *ptr);
 
   getch();
   
   return 0;
}

Output of the program:

Derived Data Types in C

Functions

A function is a self-contained block of statements that performs a specific task. A function wraps a number of statements which are repeated in a program into a single unit or block. This wrapping of statements(unit) is identified by a name known as a function name.

Every C program consists of at least one function, which is the main() function.

Program 3: Multiplication of two numbers using functions

#include<stdio.h>


int multiplication();  // function declaration


int main()
{   


   int output;   //local variable definition   
    
   output = multiplication();


    printf("The product of the two numbers is: %d\n",output);
    return 0;
}


int multiplication()
{
    int num1 = 5, num2 = 15;
    return num1*num2;
}

Output of the program:

Derived Data Types in C

Program 4:  Calculator program in C using functions

#include <stdio.h>
float addition(float x, float y);
float subtraction(float x, float y);
float multiplication(float x, float y);
float division(float x, float y);


int main()
{
	char operator;
	float num1, num2, result = 0;
	printf("\n Please Enter a valid Operator (+, -, *, /)  :  ");
          scanf("%c", &operator);
  	printf("\n Enter the values for operands: num1 and num2  :  ");
  	scanf("%f%f", &num1, &num2);
  	switch(operator)
  	{
  		case '+':
printf("\n Operator entered : Addition");
result = addition(num1, num2);
break;
case '-':


printf("\n Operator entered : Subtraction");
result = subtraction(num1, num2);


break;  			


case '*':
printf("\n Operator entered : Multiplication");
result = multiplication(num1, num2);


break;


case '/':
printf("\n Operator entered : Division");
result = division(num1, num2);


break;


default:
printf("\n You have entered an Invalid Operator ");				    			}


printf("\n The result of %.2f %c %.2f  = %.2f", num1, operator, num2, result);


return 0;


}


float addition(float x, float y)
{
return x + y;
}


float subtraction(float x, float y)
{
return x - y;
}


float multiplication(float x, float y)
{
return x * y;
}
float division(float x, float y)
{
	return x / y;
}

Output of the program:

Derived Data Types in C

Related Topics

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.