Data types in C++ are meant to identify and inform compiler the type of data, memory and associated operations of handling it. Variable in C/C++ both use data-type during declaration to restrict the type of data to be stored. Every data type requires different amount of memory allotted by compiler.
There are three types of data types:
- Primary(Built-in) Data Types
- User Defined Data Types
- Derived Data Types
Primary Data Types
Primary data types are built-in or predefined data types that can be used directly by the user to declare variables. There are six different data types in C++ and most this are same as of C language except Boolean and wide character.
This article contains very little details of this type if you need more you can study this article.
- Character: Keyword ‘char’ used to store character data type like a-z, A-Z and special symbols (@, !, %). This Characters type requires 1 byte of memory and ranges from -128 to 127 or 0 to 255.
- Integer: As the name suggests it holds only integer values requires 4 bytes of memory space and value ranging from -2147483648 to 2147483647. Keyword ‘int’ used for integer data types.
- Floating Point: Keyword ‘float’ used to stores real numbers known as Floating Point. Float type used to store single precision floating point values or decimal values. It requires 4 byte of memory space.
- void: Void data type used to represents a valueless entity. Void data type mostly used for function which does not returns a value. Please see this article for more details about above four data type.
- Boolean: Boolean data type used for storing logical values in variables. A Boolean type can store either true or false OR 0 or 1. Keyword ‘bool’ used for this type of variable.
- Wide Character: Wide character is similar to char data type but the difference is the size of wide character is greater than the normal 1 byte datatype. Generally Wide char is 2 or 4 bytes long and keyword ‘wchar_t’ used.
//program to demonstrate use of wchar_t
#include <iostream>
using namespace std;
int main()
{
wchar_t w = 'a';
wcout << "Wide character value: " << w << endl ;
// w prefixed in operations like printing (wcout) while operating on wide-char type
cout << "Size of the wide char is: " << sizeof(w);
return 0;
}
Output:
Wide character value: a Size of the wide char is: 2
Datatype size Range limit format specifier
char 1 −128 to 127 %c
signed char 1 −128 to 127 %c
unsigned char 1 0 to 255 %c
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
float 4 1.2E-38 to 3.4E+38 %f
double 8 2.3E-308 to 1.7E+308 %lf
long double 12 3.4E-4932 to 1.1E+4932 %Lf
We can display the size of all the data types by using the sizeof() function and passing the keyword of the datatype as argument .
// C++ program to show size of data types
#include<iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char)<<endl;
cout << "Size of int : " << sizeof(int)<<endl;
cout << "Size of float : " << sizeof(float)<<endl;
cout << "Size of double : " << sizeof(double)<<endl;
return 0;
}
Output:
Size of char : 1
Size of int : 4
Size of float : 4
Size of double : 8
User-Defined Data Types:
User defined data type are defined by user itself. Like, defining a class, structure, in C++.
These types include:
- Structure
- Union
- Enumeration
- Typedef
- Class
Brief description of these one by one:
struct name{
char name[10]; //data member
int R_no;
float percent;
};
union name_of_union{
int X;
float Y;
long double Z; // union size is equal to long double i.e. 12
};
enum day {monday, tuesday, wednesday, thursday, friday, saturday};
enum State {Working = 1, Failed = 0}; // explicit assignment
typedef int INTEGER;
int main()
{
INTEGER num1 = 1, num2 = 2;
printf("%d %d",num1,num2);
return 0;
}
Output:
1 2
class nameofclass{
access specifier: // public, private or protected
data members; // data variable
member functions // this function used to access data members and
//may or may not contain definition inside class
}
// Class example program
#include<iostream>
using namespace std;
class add{
int a,b;
public:
int getab(int x,int y){
a = x;
b = y;
return a+b;
}
};
int main()
{
add obj;
cout << "Add : " << obj.getab(5,4)<<endl;
return 0;
}
Output:
Add : 9
Derived Data Type
Derived data type is a data type in C++ which are built from basic data types like Float,int,double,etc.
int function() // function with int return type
int main()
{
}
#include<iostream>
using namespace std;
int main()
{
int a = 1;
// reference to a.
int& b = a;
// a is changed to 2
b = 2;
cout << "a = " << a << endl ;
// a is changed to 3
a = 3;
cout << "b = " << b << endl ;
return 0;
}
Output:
a = 2 b = 3
int main(){
int i=10;
int *j; // integer pointer
j=&i; // i address assigned to j
printf("%d %p",i,j); // %p is a format specifier for pointer
// OUTPUT will be different for you
return 0;
}
Output:
10 0060FF08
int arr1[10] = {1,2,3,4,5,6,7,8,9,10};