Разница между фундаментальными типами данных и производными типами данных
Опубликовано: 7 Января, 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++
// C++ program to illustrate array// derived data type#include <bits/stdc++.h>usingnamespacestd;// main method starts from hereintmain(){// array of size 5inta[5] = { 1, 2, 3, 4, 5 };// indexing variableinti;for(i = 0; i < 5; i++)cout << ("%d ", a[i]);return0;}Java
// Java program to illustrate// primitive data typesclassGFG {publicstaticvoidmain(String[] args){// Integer valueinta =2;// Float valuefloatb =2.0f;// Double valuedoublec =2.0003;// Characterchard ="D";System.out.printf("Integer value is = %d", a);System.out.printf(" Float value is = %f", b);System.out.printf(" Double value is = %f", c);System.out.printf(" Char value is = %c", d);}}// This code has been contributed by 29AjayKumarPython
# 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(" Float value is = ", b);print(" Double value is = ", c);print(" Char value is = ", d);# This code has been contributed by Code_MechC#
// C# program to illustrate// primitive data typesusingSystem;classGFG {publicstaticvoidMain(){// Integer valueinta = 2;// Float valuefloatb = 2.0f;// Double valuedoublec = 2.0003;// Characterchard ="D";Console.WriteLine("Integer value is = "+ a);Console.WriteLine(" Float value is = "+ b);Console.WriteLine(" Double value is = "+ c);Console.WriteLine(" Char value is = "+ d);}}// This code has been contributed by Code_Mech.PHP
<?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(" Float value is = ".$b);echo(" Double value is = ".$c);echo(" Char value is = ".$d);}// This code has been contributed by Code_Mech.Output: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 :
C++
// C++ program to illustrate pointer// as derived data type#include <iostream>usingnamespacestd;// main methodintmain(){// integer variableintvariable = 10;// Pointer for storing addressint* pointr;// Assigning address of variable to pointerpointr = &variable;cout <<"Value of variable = "<< variable;// cout << " Value at pointer = "<< pointr;cout <<" Value at *pointer = "<< *pointr;return0;}// This code is contributed by shubhamsingh10C
// C program to illustrate pointer// as derived data type#include <stdio.h>// main method starts from hereintmain(){// integer variableintvariable = 10;// Pointer for storing addressint* pointr;// Assigning address of variable to pointerpointr = &variable;printf("Value of variable = %d", variable);// printf(" Value at pointer = %d", pointr);printf(" Value at *pointer = %d", *pointr);return0;}Output:Value of variable = 10 Value at *pointer = 10
 - Array :
C++
// C++ program to illustrate array// derived data type#include <bits/stdc++.h>usingnamespacestd;// main method starts from hereintmain(){// array of size 5inta[5] = { 1, 2, 3, 4, 5 };// indexing variableinti;for(i = 0; i < 5; i++)cout << ("%d ", a[i]);return0;}// This code is contributed by Code_Mech.C
// C program to illustrate array// derived data type#include <stdio.h>// main method starts from hereintmain(){// array of size 5inta[5] = { 1, 2, 3, 4, 5 };// indexing variableinti;for(i = 0; i < 5; i++)printf("%d ", a[i]);return0;}Java
// Java program to illustrate array// derived data typeimportjava.util.*;classGFG {// Driver codepublicstaticvoidmain(String[] args){// array of size 5inta[] = {1,2,3,4,5};// indexing variableinti;for(i =0; i <5; i++)System.out.printf("%d ", a[i]);}}/* This code contributed by PrinciRaj1992 */Python3
# Python3 program to illustrate array# derived data type# Driver code# array of size 5a=[1,2,3,4,5];# indexing variableforiinrange(5):print(a[i], end=" ");# This code contributed by mitsC#
// C# program to illustrate array// derived data typeusingSystem;classGFG {// Driver codepublicstaticvoidMain(String[] args){// array of size 5int[] a = { 1, 2, 3, 4, 5 };// indexing variableinti;for(i = 0; i < 5; i++)Console.Write("{0} ", a[i]);}}// This code contributed by Rajput-JiPHP
<?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?>Output:1 2 3 4 5
 - Structures –
C++
// C++ program to illustrate structure// derived data type#include <bits/stdc++.h>usingnamespacestd;// structurestructstructure_example{intinteger;floatdecimal;charcharacter[20];};// Main Methodintmain(){structstructure_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;return0;}// This code is contributed by shubhamsingh10C
// C program to illustrate structure// derived data type#include <stdio.h>// structurestructstructure_example {intinteger;floatdecimal;charcharacter[20];};// main method starts from herevoidmain(){structstructure_example s = { 15, 98.9,"geeksforgeeks"};printf("Structure data - integer = %d decimal =%f name = %s", s.integer, s.decimal, s.character);}Output:Structure data - integer = 15 decimal = 98.900002 name = geeksforgeeks
 
 - Pointer :
 
| 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 | 
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.