Difference between fundamental data types and derived data types

In computer programming, data type is a classification that specifies to compiler or interpreter which type of data user is intending to use.
There are two types of data types –

  1. Primitive/Fundamental data type : Each variable in C/C++ has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it.
    Example of fundamental data types –

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate array
    // derived data type
    #include <bits/stdc++.h>
    using namespace std;
      
    // main method starts from here
    int main()
    {
        // array of size 5
        int a[5] = { 1, 2, 3, 4, 5 };
      
        // indexing variable
        int i;
        for (i = 0; i < 5; i++)
            cout << ("%d ", a[i]);
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // primitive data types
      
    class GFG {
      
        public static void main(String[] args)
        {
            // Integer value
            int a = 2;
      
            // Float value
            float b = 2.0f;
      
            // Double value
            double c = 2.0003;
      
            // Character
            char d = 'D';
      
            System.out.printf("Integer value is = %d", a);
            System.out.printf("\nFloat value is = %f", b);
            System.out.printf("\nDouble value is = %f", c);
            System.out.printf("\nChar value is = %c", d);
        }
    }
      
    // This code has been contributed by 29AjayKumar

    chevron_right

    
    

    Python

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to illustrate
    # primitive data types
      
    # Integer value
    a = 2
      
    # Float value
    b = 2.0
      
    # Double value
    c = 2.0003
      
    # Character
    d ='D'
    print("Integer value is = ", a);
    print("\nFloat value is = ", b);
    print("\nDouble value is = ", c);
    print("\nChar value is = ", d);
          
    # This code has been contributed by Code_Mech

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate
    // primitive data types
    using System;
      
    class GFG {
      
        public static void Main()
        {
            // Integer value
            int a = 2;
      
            // Float value
            float b = 2.0f;
      
            // Double value
            double c = 2.0003;
      
            // Character
            char d = 'D';
      
            Console.WriteLine("Integer value is = " + a);
            Console.WriteLine("\nFloat value is = " + b);
            Console.WriteLine("\nDouble value is = " + c);
            Console.WriteLine("\nChar value is = " + d);
        }
    }
      
    // This code has been contributed by Code_Mech.

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP program to illustrate 
    // primitive data types 
    {
        // Integer value 
        $a = 2; 
      
        // Float value 
        $b = 2.0; 
      
        // Double value 
        $c = 2.0003; 
      
        // Character 
        $d = 'D'
      
        echo("Integer value is = ". $a); 
        echo("\nFloat value is = ". $b); 
        echo("\nDouble value is = ". $c); 
        echo("\nChar value is = ". $d); 
    }
      
      
    // This code has been contributed by Code_Mech.

    chevron_right

    
    

    Output:



    Integer value is = 2
    Float value is = 2.000000
    Double value is = 2.000300
    Char value is = D
    
  2. Derived data type : These data types are defined by user itself. Like, defining a class in C++ or a structure. These include Arrays, Structures, Class, Union, Enumeration, Pointers etc.
    Examples of derived data type :

    • Pointer :
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C program to illustrate pointer
      // as derived data type
      #include <stdio.h>
      // main method starts from here
      int main()
      {
          // integer variable
          int variable = 10;
        
          // Pointer for storing address
          int* pointr;
        
          // Assigning address of variable to pointer
          pointr = &variable;
          printf("Value of variable = %d", variable);
        
          // printf("\nValue at pointer = %d", pointr);
          printf("\nValue at *pointer = %d", *pointr);
          return 0;
      }

      chevron_right

      
      

      Output:

      Value of variable = 10
      Value at *pointer = 10
      
    • Array :

      C++

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C++ program to illustrate array
      // derived data type
      #include <bits/stdc++.h>
      using namespace std;
      // main method starts from here
      int main()
      {
          // array of size 5
          int a[5] = { 1, 2, 3, 4, 5 };
        
          // indexing variable
          int i;
          for (i = 0; i < 5; i++)
              cout << ("%d ", a[i]);
          return 0;
      }
        
      // This code is contributed by Code_Mech.

      chevron_right

      
      

      C

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C program to illustrate array
      // derived data type
      #include <stdio.h>
      // main method starts from here
      int main()
      {
          // array of size 5
          int a[5] = { 1, 2, 3, 4, 5 };
        
          // indexing variable
          int i;
          for (i = 0; i < 5; i++)
              printf("%d  ", a[i]);
          return 0;
      }

      chevron_right

      
      

      Java

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // Java program to illustrate array
      // derived data type
      import java.util.*;
        
      class GFG {
        
          // Driver code
          public static void main(String[] args)
          {
              // array of size 5
              int a[] = { 1, 2, 3, 4, 5 };
        
              // indexing variable
              int i;
              for (i = 0; i < 5; i++)
                  System.out.printf("%d ", a[i]);
          }
      }
        
      /* This code contributed by PrinciRaj1992 */

      chevron_right

      
      

      Python3

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Python3 program to illustrate array 
      # derived data type 
        
      # Driver code 
        
      # array of size 5 
      a = [1, 2, 3, 4, 5]; 
        
      # indexing variable 
      for i in range(5): 
          print(a[i], end = " "); 
        
      # This code contributed by mits

      chevron_right

      
      

      C#

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C# program to illustrate array
      // derived data type
      using System;
        
      class GFG {
          // Driver code
          public static void Main(String[] args)
          {
              // array of size 5
              int[] a = { 1, 2, 3, 4, 5 };
        
              // indexing variable
              int i;
              for (i = 0; i < 5; i++)
                  Console.Write("{0} ", a[i]);
          }
      }
        
      // This code contributed by Rajput-Ji

      chevron_right

      
      

      PHP

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?php
      // PHP program to illustrate array 
      // derived data type 
        
      // Driver code 
        
      // array of size 5 
      $a = array(1, 2, 3, 4, 5); 
        
      // indexing variable 
      for ($i = 0; $i < 5; $i++) 
          print($a[$i] . " "); 
        
      // This code contributed by mits
      ?>

      chevron_right

      
      

      Output:

      1  2  3  4  5
      
    • Structures
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C program to illustrate structure
      // derived data type
      #include <stdio.h>
      // structure
      struct structure_example {
          int integer;
          float decimal;
          char character[20];
      };
      // main method starts from here
      void main()
      {
          struct structure_example s = { 15, 98.9, "geeksforgeeks" };
          printf("Structure data - \n integer = %d \n decimal = 
          %f \n name = %s", s.integer, s.decimal, s.character);
      }

      chevron_right

      
      

      Output:

      Structure data - 
       integer = 15 
       decimal = 98.900002 
       name = geeksforgeeks
      
Fundamental Data Types Derived Data Types
Fundamental data type is also called primitive data type. These are the basic data types. Derived data type is the aggregation of fundamental data type.
character, integer, float, and void are fundamental data types. Pointers, arrays, structures and unions are derived data types.
Character is used for characters. It can be classified as
char, Signed char, Unsigned char.
Pointers are used for storing address of variables.
Integer is used for integers( not having decimal digits). It can be classified as signed and unsigned. Further classified as int, short int and long int. Array is used to contain similar type of data.
float is used for decimal numbers. These are classified as float, double and long double. structure is used to group items of possibly different types into a single type.
void is used where there is no return value required. It is like structure but all members in union share the same memory location


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.