In this tutorial, we are going to learn about derived and user defined data types in C Language.

1. Derived Data type:

Data types that are derived from fundamental data types are called derived data types.

Derived data types do not create new data types. Instead, they add some functionality to the existing data types.

Derived data types are derived from the primitive data types by adding some extra relationships with the various elements of the primary data types. The derived data type can be used to represent a single value or multiple values.

Given below are the various derived data types used in C:

  1. Arrays: An array is an ordered sequence of finite data items of the same data type that share a common name.
  2. pointers: A pointer is a special type of variable used to hold the address of another variable.
  3. Functions: A function is a self-contained block of one or more statements with a name.
  4. Structures: A structure is a collection of different data type items stored in a contiguous memory allocation.
  5. Unions: A union is similar to a structure where the memory allocated to the largest data type is reused for other types in the group.

In some situations, structures and unions can also be called the user-defines data types.

2. User Defined Data types in C:

2.1 Structures:

A Structure is used to organize a group of related data items of different data types referring to a single entity. i.e., a single variable capable of holding data items of different data types.

The data items in a structure are usually related like different kinds of information about a person or about a part or about an account, etc. Each data item in a structure is called a member, sometimes these members are also called fields.

The keyword used to create a structure is a struct. The advantage of using a structure is that the accessibility of members becomes easier since all the members of a specific structure get the allocation of continuous memory and therefore it minimizes the memory access time.

Generally, a structure can be declared as:

struct tag_name {
  data_type1 svar_1;
  data_type2 svar_2;
  ....
  ....
  data_typen svar_n;
};

The declaration begins with the keyword struct. The list of the declaration of its members must be enclosed in braces, the tag_name is an identifier that specifies the new structure name.

The declaration of a structure does not reserve any storage space. But the definition of the structure creates structure variables.

The structure variables can be defined as:

struct tag_name svar_1, svar_2 ...svar_n;

Lets us consider an example:

struct sample { 
  int a, b;
  float c, d;
  char e, f;
}; 
struct sample v1, v2, v3; //structure definition

2.2 Union:

A union is also a collection of different data types in C but that allows to store different data types in the same memory location.

User can define a union with many members, but only one member can contain a value at any given time.

Unions provide an efficient way of using the same memory location for multiple-purpose.

A union is same as structures but the difference is that only one member can be accessed at a time because the memory is created only for one member which has the highest number of bytes in size.

A union is declared by using the keyword union and members of the union can be accessed by using dot (.) operator.

The declaration and definition of the union is:

union tag_name {
  data_type1 uvar_1; 
  data_type2 uvar_2;
  ......
  data_typen uvar_n;
};
union tag_name uvar_1, uvar_2,....uvar_n;

Lets consider an example:

union sample {
  int age;
  float price;
  char name;
};
union sample s;

In the above example 4 bytes of memory is allocated to the union variable s, the members can be accessed as s.number, s.price, s.name but only one member can be accessed at a time because the same memory is used for all the 3 members.

2.3 typedef:

The keyword typedef is used to create a new name (alias) for an existing data type. It does not create a new data type. The syntax of using typedef is as follows:

typedef  existing_type  new_data_type;

Consider the following example:

#include <stdio.h>
void main() {
     typedef int Tutorials; //statement-1
     Tutorials a = 17;
     printf("Given value =%d\n", a);
}

In statement – 1, the keyword typedef is used to create Tutorials as the alias for the int data type. From this statement onwards, Tutorials will be the new name for int in this program and the variables declared as Tutorials type will also behave like int variables for all practical purposes.

2.4 enum:

enum is a keyword used to create an enumerated data type.

An enum (enumerated data type) is a special data type consisting of a set of named values called elements or members. It is mainly used to assign names to integral constants, which makes a program more readable.

The format for creating an  enum type is

enum identifier (value1, value2, …. , valueN);

Enumerated types allow us to create our own symbolic names for a list of related constants.

For example, we could create an enumerated data type for true and false as

enum Boolean {
     false,
     true
};

If we do not explicitly assign values to enum names, the compiler assigns values starting from 0 by default.

Here, false is assigned 0, and true is assigned 1 automatically. (The first field of the enum is replaced with the value 0 and the next field with 1 and so on.)

Consider the following example using enum.

#include <stdio.h>
 void main() { 
 enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; 
 enum month birthday = JUL; 
 printf("Birthday Month = %d", birthday); 
}

The field name JAN is assigned the value 1. Hence, the next field name is automatically assigned the value 2 and so on.

The above program will print the output as follows:

Birthday Month = 7

3. Empty Data:

void:

void keyword is an empty data type that represents no value. It is used in functions and pointers.

When used in functions, the void data type does not create any variable. It is used to represent the return type of a function. User cannot declare a variable by using void as

void bad_variable;

because it does not allocate any memory space for the void type variables.

The void keyword can be used as the return type and parameter type in a function as given below:

void main(void) { }

It specifies that the main() function does not receive and return anything.

4. References:

Happy Learning 🙂