C - Derived and User Defined Data Types - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about derived and user defined data types in C Language.
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:

Arrays: An array is an ordered sequence of finite data items of the same data type that share a common name.
pointers: A pointer is a special type of variable used to hold the address of another variable.
Functions: A function is a self-contained block of one or more statements with a name.
Structures: A structure is a collection of different data type items stored in a contiguous memory allocation.
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.

Arrays: An array is an ordered sequence of finite data items of the same data type that share a common name.
pointers: A pointer is a special type of variable used to hold the address of another variable.
Functions: A function is a self-contained block of one or more statements with a name.
Structures: A structure is a collection of different data type items stored in a contiguous memory allocation.
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.
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
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.
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.
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
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.
Wiki – Data Type
C Data Types
Happy Learning 🙂

What are the Data types in C?
C – Floating Point Data Types
C – Integer Data Types – int, short int, long int and char
PHP Data types Example Tutorials
User defined Exceptions in Python
User defined sorting with Java 8 Comparator
What are different Python Data Types
Types Of Inheritance in Java
user defined exceptions in Java
Python Set Data Structure in Depth
Organization of data In Data Structures
Python TypeCasting for Different Types
Java variable types Example
Java Class Example Tutorials
What is Data Structures ?

What are the Data types in C?
C – Floating Point Data Types
C – Integer Data Types – int, short int, long int and char
PHP Data types Example Tutorials
User defined Exceptions in Python
User defined sorting with Java 8 Comparator
What are different Python Data Types
Types Of Inheritance in Java
user defined exceptions in Java
Python Set Data Structure in Depth
Organization of data In Data Structures
Python TypeCasting for Different Types
Java variable types Example
Java Class Example Tutorials
What is Data Structures ?
 


Δ
 C – Introduction
 C – Features
 C – Variables & Keywords
 C – Program Structure
 C – Comment Lines  & Tokens
 C – Number System
 C – Local and Global Variables
 C – Scope & Lifetime of Variables
 C – Data Types
 C – Integer Data Types
 C – Floating Data Types
 C – Derived, Defined Data Types
 C – Type Conversions
 C – Arithmetic Operators
 C – Bitwise Operators
 C – Logical Operators
 C –  Comma and sizeof Operators
 C – Operator Precedence and Associativity
 C – Relational Operators
 C Flow Control – if, if-else, nested if-else, if-else-if
 C – Switch Case
 C Iterative – for, while, dowhile loops
 C Unconditional – break, continue, goto statements
 C – Expressions and Statements
 C – Header Files & Preprocessor Directives
 C – One Dimensional Arrays
 C – Multi Dimensional Arrays
 C – Pointers Basics
 C – Pointers with Arrays
 C – Functions
 C – How to Pass Arrays to Functions
 C – Categories of Functions
 C – User defined Functions
 C – Formal and Actual Arguments
 C – Recursion functions
 C – Structures Part -1
 C – Structures Part -2
 C – Unions
 C – File Handling
 C – File Operations
 C – Dynamic Memory Allocation
 C Program – Fibonacci Series
 C Program – Prime or Not
 C Program – Factorial of Number
 C Program – Even or Odd
 C Program – Sum of digits till Single Digit
 C Program – Sum of digits
 C Program – Reverse of a number
 C Program – Armstrong Numbers
 C Program – Print prime Numbers
 C Program – GCD of two Numbers
 C Program – Number Palindrome or Not
 C Program – Find Largest and Smallest number in an Array
 C Program – Add elements of an Array
 C Program – Addition of Matrices
 C Program – Multiplication of Matrices
 C Program – Reverse of an Array
 C Program – Bubble Sort
 C Program – Add and Sub without using + –