📜  基本数据类型和派生数据类型之间的区别

📅  最后修改于: 2021-05-26 00:51:21             🧑  作者: Mango

在计算机编程中,数据类型是一种分类,用于向编译器或解释器指定用户打算使用哪种数据类型。
有两种类型的数据类型–

  1. 原始/基本数据类型:C / C++中的每个变量都有一个关联的数据类型。每种数据类型需要不同数量的内存,并具有一些可以在其上执行的特定操作。
    基本数据类型的示例–
    C++
    // C++ program to illustrate array
    // derived data type
    #include 
    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;
    }


    Java
    // 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
    # 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


    C#
    // 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.


    PHP


    C++
    // C++ program to illustrate pointer
    // as derived data type
    #include 
    using namespace std;
      
    // main method 
    int 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
    // C program to illustrate pointer
    // as derived data type
    #include 
    // 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;
    }


    C++
    // C++ program to illustrate array
    // derived data type
    #include 
    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.


    C
    // C program to illustrate array
    // derived data type
    #include 
    // 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;
    }


    Java
    // 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 */


    Python3
    # 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


    C#
    // 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


    PHP


    C++
    // C++ program to illustrate structure
    // derived data type
    #include 
    using namespace std;
      
    // structure
    struct structure_example 
    {
        int integer;
        float decimal;
        char character[20];
    };
      
    // Main Method 
    int main()
    {
        struct structure_example s = { 15, 98.9, "geeksforgeeks" };
        cout << "Structure data -"<< endl;
        cout << "integer = " << s.integer << endl;
        cout << fixed<C
    
    // C program to illustrate structure
    // derived data type
    #include 
    // 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);
    }


    输出:
    Integer value is = 2
    Float value is = 2.000000
    Double value is = 2.000300
    Char value is = D
    
  2. 派生数据类型:这些数据类型由用户自己定义。就像在C++中定义类或结构一样。这些包括数组,结构,类,联合,枚举,指针等。
    派生数据类型的示例:
    • 指针:

      C++

      // C++ program to illustrate pointer
      // as derived data type
      #include 
      using namespace std;
        
      // main method 
      int 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

      // C program to illustrate pointer
      // as derived data type
      #include 
      // 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;
      }
      
      输出:
      Value of variable = 10
      Value at *pointer = 10
      
    • 大批 :

      C++

      // C++ program to illustrate array
      // derived data type
      #include 
      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.
      

      C

      // C program to illustrate array
      // derived data type
      #include 
      // 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;
      }
      

      Java

      // 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 */
      

      Python3

      # 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
      

      C#

      // 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
      

      的PHP

      
      
      输出:
      1  2  3  4  5
      
    • 结构

      C++

      // C++ program to illustrate structure
      // derived data type
      #include 
      using namespace std;
        
      // structure
      struct structure_example 
      {
          int integer;
          float decimal;
          char character[20];
      };
        
      // Main Method 
      int main()
      {
          struct structure_example s = { 15, 98.9, "geeksforgeeks" };
          cout << "Structure data -"<< endl;
          cout << "integer = " << s.integer << endl;
          cout << fixed< C 
      
      // C program to illustrate structure
      // derived data type
      #include 
      // 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);
      }
      
      输出:
      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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”