Datatype in C++
Data Type in C++
Data type is a keyword used to identify type of data. It is used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer.
In general every programming language is containing three categories of data types. They are
- Fundamental or primitive data types
- Derived data types
- User defined data types
Primitive data types
Primitive Data Types: These are the data types whose variable can hold maximum one value at a time, in C++ language it can be achieve by int, float, double, char. These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are:
- Integer
- Character
- Boolean
- Floating Point
- Double Floating Point
- Valueless or Void
- Wide Character
Example
int a; // valid a = 10,20,30; // invalid
Derived data types
Derived Data Types: These data type are derived from fundamental data type. Variables of derived data type allow us to store multiple values of same type in one variable but never allows to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In C++ language it can be achieve by array. The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely;
- Function
- Array
- Pointer
- Reference
Example
int a[] = {10,20,30}; // valid int b[] = {100, 'A', "ABC"}; // invalid
User defined data types
User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in C++ language it is achieved by structure.
Abstract or User-Defined Data Types These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes
- Class
- Structure
- Union
- Enumeration
- Typedef defined DataType
Syntax
struct emp { int id; char ename[10]; float sal; };
In C++ language, user defined data types can be developed by using struct, union, enum etc.
Syntax
// C++ program to sizes of data types #include<iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << " byte" << endl; cout << "Size of int : " << sizeof(int) << " bytes" << endl; cout << "Size of short int : " << sizeof(short int) << " bytes" << endl; cout << "Size of long int : " << sizeof(long int) << " bytes" << endl; cout << "Size of signed long int : " << sizeof(signed long int) << " bytes" << endl; cout << "Size of unsigned long int : " << sizeof(unsigned long int) << " bytes" << endl; cout << "Size of float : " << sizeof(float) << " bytes" <<endl; cout << "Size of double : " << sizeof(double) << " bytes" << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" <<endl; return 0; }
Output
Size of char : 1 byte Size of int : 4 bytes Size of short int : 2 bytes Size of long int : 8 bytes Size of signed long int : 8 bytes Size of unsigned long int : 8 bytes Size of float : 4 bytes Size of double : 8 bytes Size of wchar_t : 4 bytes