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.
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.
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)|
||Integer||2 or 4|
Now, let us discuss these Primitivedata types in more detail.
1. C++ int
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.
int age = 20;
2. C++ float and double
double are used to store floating-point numbers (decimals and exponentials).
float variable typically requires 4 bytes of memory space and
double variable requires 8 bytes of memory space.
double has two times the precision of
float area = 14.34; double volume = 34.6534;
As mentioned above, these two data types are also used for exponentials.
double distance = 35E11 // 35E11 is equal to 35*10^11
3. C++ char
char is used for storing characters.
2) Character typically requires 1 byte of memory space.
3) Characters in C++ are enclosed inside single quotes
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
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
char32_t introduced in C++11.
5. C++ bool
1) Bool data type is used for storing boolean or logical values.
bool data type can store either
3) Booleans are used in conditional statements and loops (which we will learn in later chapters).
6. C++ void
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
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:
We can modify the following data types with the above modifiers:
C++ Modified Data Types List
|Data Type||Size (in Bytes)||Meaning|
||4||used for integers (equivalent to
||4||can only store positive integers|
||2||used for small integers (range -32768 to 32767)|
||at least 4||used for large integers (equivalent to
||4||used for large positive integers or 0 (equivalent to
||8||used for very large integers (equivalent to
||8||used for very large positive integers or 0 (equivalent to
||8||used for large floating-point numbers|
||1||used for characters (guaranteed range -127 to 127)|
||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 :-
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 :-
5) Typedef defined Data Type
We will learn about these User-defined data types in later tutorials.
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++