let get some prior knowledge of variables before going to data types in data structures. Let us relate to that old mathematical equations. As an equations consider this example : x^2 -4y-5 = 0. Here the equations has names x and y which hold a certain value. You can say this x and y are placeholders for representing data. Similarly you have Variables in Programming to store data.
Data types –
In above example x and y can take any values such as integral values, real numbers. A data type in a programming language is a set of data with predefined values. Example of data types are: integer, float, byte, character, string, etc.
Computer memory is filled with zeros and ones. This is difficult to provide solution of any problem in zeros and ones. So programming language and compilers provide us these data types. for example : integer takes 2 bytes, float takes 4 bytes (these depends on compiler to compiler). A data types reduces coding effort.
There are two types of data types :
- System-defined data types (Primitive data types)
- User defined data types
System-defined data types (Primitive data types) –
Data types that are defined by systems called as primitive data types. Most common primitive data types are int, float, char, double, bool, etc. The number of bits allocated to data types differ with programming language and compiler.
User-defined data types –
If the system-defined data types are not enough, then most programming language allow users to define their own data types, called as user-defined data types. Good examples of user defined data types are structures in C/C++ and classes in Java. Below we are combining two system-defined data types and naming it as “nType”.
struct nType{
int num1;
float num2;
}
This gives more comfort while dealing with computer memory.