C++ Data Types
C++ Data Types
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.
Type Keyword
Boolean bool
Character char
Integer int
Valueless void
Several of the basic types can be modified using one or more of these type modifiers
−
signed
unsigned
:
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.
Typical Bit
Type Typical Range
Width
-9223372036854775808 to
long int 8bytes
9223372036854775807
unsigned long
8bytes 0 to 18,446,744,073,709,551,615
long int
:
float 4bytes
double 8bytes
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.
Open Compiler
#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
Open Compiler
#include <iostream>
#include <limits>
using namespace std;
int main() {
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
Example
Open Compiler
#include <iostream>
:
using namespace std;
Output
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
:
Open Compiler
#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
12321
3. Pointer
Syntax
Example
:
Open Compiler
#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
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
Example
:
Open Compiler
#include <iostream>
using namespace std;
int main(){
int c=11;
int& refer=c;
refer=121;
cout<<"After changing value using refer variable :"<<c<<endl;
return 0;
}
Output
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
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
Open Compiler
#include <iostream>
using namespace std;
class TP{
public:
string tp;
void print(){
cout<<tp<<endl;
}
:
};
int main(){
TP object;
object.tp="I Love Tutorialspoint !!!";
object.print();
return 0;
}
Output
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
Open Compiler
#include <iostream>
:
using namespace std;
struct TP{
string tp;
int grade;
};
int main(){
TP object;
object.tp="I Love Tutorialspoint !!!";
object.grade=10;
cout<<object.tp<<endl;
cout<<"How much would you rate it?"<<" : "<< object.grade;
return 0;
}
Output
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
Open Compiler
#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
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
Open Compiler
#include <iostream>
using namespace std;
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
For example, the following tells the compiler that feet is another name for int −
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 −
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.
Here, blue will have a value of 6 because each name will be one greater than the
one that precedes it.
: