C++ Data Types - Abdul Rehman
C++ Data Types - Abdul Rehman
In C++, data types are declarations for variables. This determines the type and size of data associated with
variables. For example,
Here, age is a variable of type int . Meaning, the variable can only store integers of either 2 or 4 bytes.
int Integer 2 or 4
float Floating-point 4
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0
1. C++ int
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
For example,
float and double are used to store floating-point numbers (decimals and exponentials).
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision
of float . To learn more, visit C++ float and double.
For example,
float area = 64.74;
double volume = 134.64534;
As mentioned above, these two data types are also used for exponentials. For example,
3. C++ char
For example,
Note: In C++, an integer value is stored in a char variable rather than the character itself. To learn more, visit
C++ characters (https://www.programiz.com/cpp-programming/char-type).
4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
It is used to represent characters that require more memory to represent them than a single char .
For example,
Note: There are also two other fixed-size character types char16_t and char32_t introduced in C++11.
5. C++ bool
The bool data type has one of two possible values: true or false .
Booleans are used in conditional statements and loops (which we will learn in later chapters).
For example,
6. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no value".
1. signed
2. unsigned
3. short
4. long
We can modify the following data types with the above modifiers:
int
double
char
Size (in
Data Type Meaning
Bytes)
long long 8 used for very large integers (equivalent to long long int ).
long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short d = 3434233; // Error! out of range
unsigned int a = -5; // Error! can only store positive numbers or 0
Derived Data Types
Data types that are derived from fundamental data types are derived types. For example: arrays, pointers,
function types, structures, etc.