C++ Data Types
While writing program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types −
Type | Keyword |
---|---|
Boolean | bool |
Character | char |
Integer | int |
Floating point | float |
Double floating point | double |
Valueless | void |
Wide character | wchar_t |
Several of the basic types can be modified using one or more of these type modifiers −
- signed
- unsigned
- short
- long
The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.
Type | Typical Bit Width | Typical Range |
---|---|---|
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 8bytes | -9223372036854775808 to 9223372036854775807 |
signed long int | 8bytes | same as long int |
unsigned long int | 8bytes | 0 to 18446744073709551615 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | |
double | 8bytes | |
long double | 12bytes | |
wchar_t | 2 or 4 bytes | 1 wide character |
The size of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
Example
Following is the example, which will produce correct size of various data types on your computer.
#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
This example uses endl, which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() operator to get size of various data types.
When the above code is compiled and executed, it produces the following result which can vary from machine to machine −
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
Example
Following is another example:
#include <iostream>
#include <limits>
using namespace std;
int main() {
std::cout << "Int Min " << std::numeric_limits<int>::min() << endl;
std::cout << "Int Max " << std::numeric_limits<int>::max() << endl;
std::cout << "Unsigned Int Min " << std::numeric_limits<unsigned int>::min() << endl;
std::cout << "Unsigned Int Max " << std::numeric_limits<unsigned int>::max() << endl;
std::cout << "Long Int Min " << std::numeric_limits<long int>::min() << endl;
std::cout << "Long Int Max " << std::numeric_limits<long int>::max() << endl;
std::cout << "Unsigned Long Int Min " << std::numeric_limits<unsigned long int>::min() <<endl;
std::cout << "Unsigned Long Int Max " << std::numeric_limits<unsigned long int>::max() << endl;
}
Derived Data Types
Data types which are obtained from pre-defined data types in C++ are known as Derived Data Types. These can be classified into four categories, namely −
1. Function
A function is the simplest form of user-defined data type. It includes a return type, a function name and input parameters.
Syntax
return_type function_name(input_param1, input_param2…){
<function_body>
}
Example
#include <iostream>
using namespace std;
string func(int n){
//returns if n is odd or even
if(n%2) return "Given number is Odd !";
else return "Given number is Even !";
}
int main(){
int a;
//enter a number
cin>>a;
cout<<func(a);
//a simple function to check if
//number is odd or even
return 0;
}
Output
Given number is Even !
2. Array
An array is a series of elements of same data type. Elements of an array are stored in contiguous memory locations in the storage.
Syntax
data_type array_name[array_size];
Example
#include <iostream>
using namespace std;
int main(){
int arr[5]={1,2,3,2,1};
//define an integer array of size 5
for(auto it:arr)
cout<<it<<" ";
//print the elements of array
return 0;
}
Output
1 2 3 2 1
3. Pointer
A pointer is a reference to an element defined previously. The value of the pointer returns the address location of the element which is associated with it.
Syntax
data_type * pointer_name=& variable_name;
Example
#include <iostream>
using namespace std;
int main() {
int a=20;
//declare variable a
int *p= &a;
//assign pointer to a
cout<<"Address of variable a: "<<p<<endl;
cout<<"Value of variable a: "<<*p<<endl;
return 0;
}
Output
Address of variable a: 0x7ffc49a8637c
Value of variable a: 20
4. Reference
A reference variable is used to create a copy of a variable with the same reference. Hence, changes made to the reference variable also reflect on the original variable.
Syntax
data_type & reference_name= variable_name;
Example
#include <iostream>
using namespace std;
int main(){
int c=11;
int& refer=c;
cout<<"Initially value of integer is: "<<c<<endl;
refer=121;
cout<<"After changing value using refer variable :"<<c<<endl;
return 0;
}
Output
Initially value of integer is: 11
After changing value using refer variable :121
User-Defined Data Types
Data types which are defined by the user intuitively without using any pre-defined data types are known as User-Defined Data Types. These data types can be further categorized into five types, namely −
1. Class
A class is a defined in Object Oriented Programming as a custom data type which is used to construct an object. It is the framework of an object, and it can include constructors, methods and OOP concepts like Polymorphism, Inheritance, etc.
Syntax
class Class_name{
<class body>
class_name(parameters) {
<constructor body>
}
return_type method_name(paremeters){
<method body>
}
}
Example
#include <iostream>
using namespace std;
class TP{
public:
string tp;
void print(){
cout<<tp<<endl;
}
};
int main(){
TP object;
object.tp="I Love TheMAK Pro !!!";
object.print();
return 0;
}
Output
I Love TheMAK Pro !!!
2. Structure (struct)
In structure data type, the user can introduce multiple primitive data types inside the struct body.
Syntax
struct struct_name{
data_type1 var_name1;
data_type2 var_name2;
…
}
Example
#include <iostream>
using namespace std;
struct TP{
string tp;
int grade;
};
int main(){
TP object;
object.tp="I Love TheMAK Pro !!!";
object.grade=10;
cout<<object.tp<<endl;
cout<<"How much would you rate it?"<<" : "<< object.grade;
return 0;
}
Output
I Love TheMAK Pro !!!
How much would you rate it? : 10
3. Union
Union is similar to a structure. In this, the memory location of all variables is same, and all variables share the same reference. Hence, a change in one value leads to all other values getting changed.
Syntax
union union_name{
data_type var_name1;
data_type var_name2;
};
Example
#include <iostream>
using namespace std;
union TP{
int tp1,tp2;
};
int main(){
union TP t;
t.tp1=2;
cout<<"Value of tp1 initially: "<<t.tp1<<endl;
t.tp2=4;
cout<<"When we change tp2, value of tp1 is : "<<t.tp1<<endl;
return 0;
}
Output
Value of tp1 initially: 2
When we change tp2, value of tp1 is : 4
4. Enumeration (Enum)
Enumeration or simply enum is a user-defined data type that is used to give name to integer constants in a program. This increases the user-readability of a program.
Syntax
enum enum_name{
var¬_name1 , var_name2, …
}
Example
#include <iostream>
using namespace std;
enum TP{ C, Java, Python, Ruby, Kotlin, Javascript, TypeScript, Others};
int main(){
enum TP course;
cout<<"Which course do you love the most?"<<endl;
course=Kotlin;
cout<<"I love the "<<course+1<<"th course !!!";
return 0;
}
Output
Which course do you love the most?
I love the 5th course !!!
typedef Declarations
You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef −
typedef type newname;
For example, the following tells the compiler that feet is another name for int −
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer variable called distance −
feet distance;
Enumerated Types
An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.
Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is −
enum enum-name { list of names } var-list;
Here, the enum-name is the enumeration’s type name. The list of names is comma separated.
For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value “blue”.
enum color { red, green, blue } c;
c = blue;
By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But you can give a name, a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.
enum color { red, green = 5, blue };
Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.