Every programming language has a set of different data types. In this article, we will learn about data types in C++.
What are data types?
A data type tells a variable the kind and size of data it can store. When we declare a variable, the compiler allocates memory for it on the basis of its data type.
In C++, there are three broad categories of data types namely,
- Fundamental data types
- Derived data types
- User-defined data types
We will mainly cover fundamental data types in this article.
Fundamental Data Types in C++
Fundamental (also called Primary or Primitive) data types are the basic built-in or predefined data types that we can directly use in our programs.
These are of the following types:
Data Type | Keyword | Size (in Bytes) |
Integer | int | 4 |
Character | char | 1 |
Floating Point | float | 4 |
Double Floating Point | double | 8 |
Boolean | bool | 1 |
Void | void | 0 |
Wide Character | wchar_t | 2 or 4 |
1. Integer: C++ int
In C++, int keyword is used for integer data type. It is generally 4 bytes in size ranging from -2147483648 to 2147483647.
For Example:
int age = 18;
2. Character: C++ char
Characters are represented using the char keyword in C++. It requires 1 byte of memory space. Its range is from -128 to 127 or 0 to 255.
While declaring a character variable, we need to enclose the character within single quotes (‘ ’). For example,
char answer = ‘y’;
3. Floating Point: C++ float
We use float to represent floating point (decimal and exponential) values in C++. It is also known as single-precision floating point data type. float data type requires 4 bytes of memory space.
For Example:
float area = 34.65;
4. Double Floating Point: C++ double
In C++, we use both float and double to store floating point numbers. But, double has twice the precision of float and its size is 8 bytes. Hence, it is also called double-precision floating point data type.
For Example:
double volume = 127.4935;
double value = 25E11; //Exponential 25E9 = 25 * 10^11
5. Boolean: C++ bool
We use bool to store boolean values, which means they can either be true or false. Size of bool is 1 byte.
For Example:
bool condition = true;
6. Void: C++ void
Void means no value. We use void for representing absence of data or valueless entities. It is usually used with functions that do not return any value.
7. Wide Character: C++ wchar_t
Wide character data type also represents characters. We use it for characters that require more than 8 bits. Its size is usually 2 or 4 bytes.
For Example:
wchart_t var = L’ם’; //var = 1501
sizeof() operator in C++
To determine the size of a data type or a variable, we can use sizeof() operator.
Example to show the use of sizeof() operator
#include <iostream> using namespace std; int main() { int age = 10; double distance = 40.5639; char answer = 'n'; bool condition = false; //displaying size of declared variables cout<<"size of variable age = "<<sizeof(age)<<endl; cout<<"size of variable distance = "<<sizeof(distance)<<endl; cout<<"size of variable answer = "<<sizeof(answer)<<endl; cout<<"size of variable condition = "<<sizeof(condition)<<endl; //displaying size of data types cout<<"size of data type float = "<<sizeof(float)<<endl; cout<<"size of data type wchar_t = "<<sizeof(wchar_t); return 0; }
Output
Size of variable distance = 8
size of variable answer = 1
size of variable condition = 1
size of data type float = 4
size of data type wchar_t = 4
Data type Modifiers
We can modify some of the fundamental data types using modifiers with them. C++ offers 4 modifiers:
1. signed
2. unsigned
3. short
4. long
Here’s a list of C++ modified data types.
Data type | Size (in Bytes) | Description | Example |
signed int / int | 4 | Stores integers | signed int n = -40; |
unsigned int | 4 | Stores 0 and positive integers | unsigned int n = 40; |
short / signed short | 2 | Equivalent to short int or signed short int, stores small integers ranging from -32768 to 32767 | short n = -2; |
unsigned short | 2 | Equivalent to unsigned short int, stores 0 and small positive integers ranging from 0 to 65535 | unsigned short n = 2; |
long | 4 | Equivalent to long int, stores large integers | long n = 4356; |
unsigned long | 4 | Equivalent to unsigned long int, stores 0 and large positive integers | unsigned long n = 562; |
long long | 8 | Equivalent to long long int, stores very large integers | long long n = -243568; |
unsigned long long | 8 | Equivalent to unsigned long long int, stores 0 and very large positive integers | unsigned long long n = 12459; |
long double | 12 | Stores large floating-point values | long double n = 432.6781; |
signed char / char | 1 | Stores characters ranging from -128 to 127 | signed char ch = ‘b’; |
unsigned char | 1 | Stores characters ranging from 0 to 255 | unsigned char ch = ‘g’; |
Derived Data Types in C++
Data types that are derived from fundamental data types are called derived data types. These are of four types in C++ namely, Function, Array, Pointer and Reference.
User-defined Data Types in C++
We, as users, can define data types. These data types defined by the user are referred to as user-defined data types. Class, Structure, Union, Enumeration and Typedef defined data type belong to this category.
typedef defined data type
With keyword typedef, we can give a C++ data type a new name.
Syntax:
typedef type newname;
Example of typedef
#include <iostream> using namespace std; typedef int integer; //we can use integer in place of int int main() { integer total = 100; cout<<total; return 0; }
Output
We have covered Derived and User-defined data types in detail in later articles.
Summary
Data types are an essential part of any programming language. Data type declares the type and size of data a variable can store. In C++, data types are broadly classified into fundamental, derived and user-defined data types.
From this article, we learnt fundamental data types in detail. These fundamental data types can be modified using Modifiers in C++.