C++ Data Types
In this tutorial, we will learn about basic data types such as int, float, char, etc. in C++ programming with the help of examples.
Data Types
1) Data types define the type of data a variables can hold.
2) This determines the type and size of data associated with variables.
3) Every data type requires a different amount of memory.
For example,
int number = 20;
Here, age is a variable of type int
. Meaning, the variable can only store integers of either 2 or 4 bytes.
C++ Types of Data Types
There are 3 types of data types in C++:
1) Primary (Primitive) Data Types
2) Derived Data Types
3) User Defined Data Types
C++ Primitive Data Types
These data types are build-in or predefined data types and can be used directly by the user to declare variables.
The table below shows the Primitive data types, their meaning, and their sizes (in bytes):
Data Type | Meaning | Size (in Bytes) |
---|---|---|
int |
Integer | 2 or 4 |
float |
Floating-point | 4 |
double |
Double Floating-point | 8 |
char |
Character | 1 |
wchar_t |
Wide Character | 2 |
bool |
Boolean | 1 |
void |
Empty | 0 |
Now, let us discuss these Primitivedata types in more detail.
1. C++ int
1) The int
keyword is used to indicate integers.
2) Integers typically requires 4 bytes of memory space.
3) It can store values from -2147483648 to 2147483647.
For example,
int age = 20;
2. C++ float and double
1) float
and double
are used to store floating-point numbers (decimals and exponentials).
2) float
variable typically requires 4 bytes of memory space and double
variable requires 8 bytes of memory space.
3) double
has two times the precision of float
.
For example:-
float area = 14.34;
double volume = 34.6534;
As mentioned above, these two data types are also used for exponentials.
For example,
double distance = 35E11 // 35E11 is equal to 35*10^11
3. C++ char
1) Keyword char
is used for storing characters.
2) Character typically requires 1 byte of memory space.
3) Characters in C++ are enclosed inside single quotes ' '
.
For example,
char car = 'c';
Note: In C++, an integer value is stored in a char
variable rather than the character itself.
4. C++ wchar_t
1) Wide character wchar_t
is similar to the char
data type, except its size is 2 bytes instead of 1.
2) It is generally 2 or 4 bytes long.
3) It is used to represent characters that require more memory to represent them than a single char
.
For example:-
wchar_t test = L'a' // storing Hebrew character;
Notice the letter L before the quotation marks, to make wide-character we have to add 'L' before the character literal.
Note: There are also two other fixed-size character types char16_t
and char32_t
introduced in C++11.
5. C++ bool
1) Bool data type is used for storing boolean or logical values.
2) The bool
data type can store either true
or false
.
3) Booleans are used in conditional statements and loops (which we will learn in later chapters).
6. C++ void
1) The void
keyword indicates an absence of data. It means "nothing" or "no value".
2) Void data type is used for those function which does not returns a value.
3) We will use void when we learn about functions and pointers.
Note: We cannot declare variables of the void
type.
C++ Type Modifiers
We can further modify some of the fundamental data types by using type modifiers. There are 4 type modifiers in C++. They are:
1) signed
2) unsigned
3) short
4) long
We can modify the following data types with the above modifiers:
1) int
2) double
3) char
C++ Modified Data Types List
Data Type | Size (in Bytes) | Meaning |
---|---|---|
signed int |
4 | used for integers (equivalent to int ) |
unsigned int |
4 | can only store positive integers |
short |
2 | used for small integers (range -32768 to 32767) |
long |
at least 4 | used for large integers (equivalent to long int ) |
unsigned long |
4 | used for large positive integers or 0 (equivalent to unsigned long int ) |
long long |
8 | used for very large integers (equivalent to long long int ). |
unsigned long long |
8 | used for very large positive integers or 0 (equivalent to unsigned long long int ) |
long double |
8 | used for large floating-point numbers |
signed char |
1 | used for characters (guaranteed range -127 to 127) |
unsigned char |
1 | used for characters (range 0 to 255) |
Let's see a few examples.
long a = 9325232;
long int d = 2315312;
long double b = 231414.26313;
short e = 1264233; // Error! out of range
unsigned int c = -9; // Error! can only store positive numbers or 0
Derived Data Types
Data types that are derived from primitive or build-in data types are referred to as derived data types.
There are 4 types of derived data types :-
1) Function
2) Array
3) Pointer
4) Reference
We will learn about these derived data types in later tutorials.
User Defined Data Types
These data types are defined by user itself. User defined data type is also known as Abstract data types.
There are 4 types of User defined data types :-
1) Class
2) Structure
3) Union
4) Enumeration
5) Typedef defined Data Type
We will learn about these User-defined data types in later tutorials.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Data Types in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Basic I/O
.