C++ Data Types
C++ provides various data types to store different types of values. The size of the data type determines the amount of memory that is allocated for a particular variable. Let's look at some of the most commonly used data types in C++.
Integer Data Types
int
: This is used for storing integer values. It can store both positive and negative whole numbers. The size of an int
data type can be either 2 bytes or 4 bytes, depending on the compiler and the operating system.
1int x = 10; 2int y = -20;
Floating-point Data Types
float
: This data type is used for storing floating-point values. It can store real numbers with a single precision. The size of a float
data type is 4 bytes.
1float x = 10.5; 2float y = -20.6;
double
: This data type is also used for storing floating-point values. The difference between float
and double
is that double
can store real numbers with double precision. The size of a double
data type is 8 bytes.
1double x = 10.5; 2double y = -20.6;
Character Data Types
char
: This data type is used for storing single characters. The size of a char
data type is 1 byte.
1char x = 'A'; 2char y = 'B';
wchar_t
: This data type is used for storing wide characters. The size of a wchar_t
data type is 2 bytes.
1wchar_t x = L'A'; 2wchar_t y = L'B';
Boolean Data Types
bool
: This data type is used for storing Boolean values (true
or false
). The size of a bool
data type is 1 byte.
1bool x = true; 2bool y = false;
Empty Data Types
void
: This data type is used to represent an empty value. It doesn't have a size.
1void x;
C++ Type Modifiers
C++ provides type modifiers to change the size and range of values that can be stored in a variable. Some of the type modifiers are:
short
long
signed
unsigned
For example, short int
, long int
, signed int
, and unsigned int
are different data types that store integer values with different sizes and ranges.