Difference between fundamental data types and derived data types

25 Mar, 2022
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 – 
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++
Java
Python
C#
PHP
Javascript
// C++ program to illustrate// primitive data types#include <bits/stdc++.h>using namespace std; // main method starts from hereint main(){    int a = 2;       float b = 2.0;       double c = 2.0003;           char d = 'D';       cout<<"Integer value is = "<< a        <<"\nFloat value is = "<< b        <<"\nDouble value is = "<< c        <<"\nChar value is = "<< d <<endl;}   // This code has been contributed by cmaggio
// 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
# Python program to illustrate# primitive data types # Integer valuea = 2 # Float valueb = 2.0 # Double valuec = 2.0003 # Characterd ='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
// C# program to illustrate// primitive data typesusing 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.
<?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.
<script> // JavaScript program to illustrate// primitive data types    // Integer value        var a = 2;         // Float value        var b = 2.0;         // Double value        var c = 2.0003;         // Character        var d = 'D';         document.write("Integer value is = ", a + "<br>");        document.write("\nFloat value is = ", b + "<br>");        document.write("\nDouble value is = ", c + "<br>");        document.write("\nChar value is = ", d + "<br>"); // This code has been contributed by shivanisinghss2110 </script>
Integer value is = 2
Float value is = 2.000000
Double value is = 2.000300
Char value is = D
 
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 :
Pointer :
C++
C
// C++ program to illustrate pointer// as derived data type#include <iostream>using namespace std; // main methodint main(){    // integer variable    int variable = 10;     // Pointer for storing address    int* pointr;     // Assigning address of variable to pointer    pointr = &variable;    cout << "Value of variable = " <<  variable;     // cout << "\nValue at pointer = "<<  pointr;    cout << "\nValue at *pointer = "<< *pointr;    return 0;} // This code is contributed by shubhamsingh10
// C program to illustrate pointer// as derived data type#include <stdio.h>// main method starts from hereint 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;}
Value of variable = 10
Value at *pointer = 10
 
Array : 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to illustrate array// derived data type#include <bits/stdc++.h>using namespace std;// main method starts from hereint 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.
// C program to illustrate array// derived data type#include <stdio.h>// main method starts from hereint 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;}
// Java program to illustrate array// derived data typeimport 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 */
# Python3 program to illustrate array# derived data type # Driver code # array of size 5a = [1, 2, 3, 4, 5]; # indexing variablefor i in range(5):    print(a[i], end = " "); # This code contributed by mits
// C# program to illustrate array// derived data typeusing 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
<?php// PHP program to illustrate array// derived data type // Driver code // array of size 5$a = array(1, 2, 3, 4, 5); // indexing variablefor ($i = 0; $i < 5; $i++)    print($a[$i] . " "); // This code contributed by mits?>
<script> // Javascript program to illustrate// array derived data type // Array of size 5let a = [ 1, 2, 3, 4, 5 ]; // Indexing variablelet i;for(i = 0; i < 5; i++)    document.write(a[i] + " ");     // This code is contributed by decode2207 </script>
1  2  3  4  5
 
Structures –
C++
C
// C++ program to illustrate structure// derived data type#include <bits/stdc++.h>using namespace std; // structurestruct structure_example{    int integer;    float decimal;    char character[20];}; // Main Methodint main(){    struct structure_example s = { 15, 98.9, "geeksforgeeks" };    cout << "Structure data -"<< endl;    cout << "integer = " << s.integer << endl;    cout << fixed<<setprecision(6)<< "decimal = " << s.decimal << endl;    cout << "name = " << s.character << endl;    return 0;} // This code is contributed by shubhamsingh10
// C program to illustrate structure// derived data type#include <stdio.h>// structurestruct structure_example {    int integer;    float decimal;    char character[20];};// main method starts from herevoid 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);}
Structure data - 
 integer = 15 
 decimal = 98.900002 
 name = geeksforgeeks
 
 
29AjayKumar
Code_Mech
princiraj1992
Rajput-Ji
Mithun Kumar
SHUBHAMSINGH10
decode2207
shivanisinghss2110
cmaggio
CBSE - Class 11
Picked
C Language
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.