C++ is a strongly typed language, which means that every variable has a specific data type that determines the type and size of the value it can hold. Numeric data types are used to represent numbers in C++. In this article, we will discuss the different numeric data types available in C++, their size, and ranges.
Integer Data Types
Integer data types are used to represent whole numbers in C++. The following are the different integer data types in C++:
-
int
: The most commonly used integer data type in C++. It can hold a wide range of whole numbers. The size ofint
varies depending on the compiler and system architecture, but it is usually 4 bytes.
#include <iostream> using namespace std; int main() { int x = 10; cout << "Value of x: " << x << endl; cout << "Size of int: " << sizeof(x) << " bytes" << endl; return 0; } // Output: "Value of x: 10" "Size of int: 4 bytes"
-
short int
orshort
: A smaller integer data type that takes up less memory thanint
. It is usually 2 bytes.
#include <iostream> using namespace std; int main() { short int y = 10; cout << "Value of y: " << y << endl; cout << "Size of short int: " << sizeof(y) << " bytes" << endl; return 0; } // Output: "Value of y: 10" "Size of short int: 2 bytes"
-
long int
orlong
: A larger integer data type that can hold a wider range of whole numbers thanint
. It is usually 4 bytes.
#include <iostream> using namespace std; int main() { long int z = 1000000; cout << "Value of z: " << z << endl; cout << "Size of long int: " << sizeof(z) << " bytes" << endl; return 0; } // Output: "Value of z: 1000000" "Size of long int: 4 bytes"
-
long long int
orlong long
: An even larger integer data type that can hold even wider range of whole numbers thanlong
. It is usually 8 bytes.
#include <iostream> using namespace std; int main() { long long int a = 1000000000000000000; cout << "Value of a: " << a << endl; cout << "Size of long long int: " << sizeof(a) << " bytes" << endl; return 0; }
Floating Point Data Types
In C++, floating point data types are used to represent decimal numbers. They are stored as binary fractions and have a fixed precision, which means that they can represent a limited number of decimal places. The two most commonly used floating point data types in C++ are float
and double
.
-
float
: A single precision floating point data type that can hold decimal numbers with a precision of 6-9 decimal places. It is usually 4 bytes.
#include <iostream> using namespace std; int main() { float a = 3.14159265358979323846; cout << "Value of a: " << a << endl; cout << "Size of float: " << sizeof(a) << " bytes" << endl; return 0; }
-
double
: A double precision floating point data type that can hold decimal numbers with a precision of 15-18 decimal places. It is usually 8 bytes.
#include <iostream> using namespace std; int main() { double a = 3.14159265358979323846; cout << "Value of a: " << a << endl; cout << "Size of double: " << sizeof(a) << " bytes" << endl; return 0; }
Boolean Data Type
In C++, the boolean data type is used to represent true or false values. It can only take two values, true or false. The keyword for boolean data type is bool
.
#include <iostream> using namespace std; int main() { bool a = true; cout << "Value of a: " << a << endl; cout << "Size of bool: " << sizeof(a) << " bytes" << endl; return 0; }
Conclusion
C++ provides a wide range of data types to suit the various needs of different types of applications. The most commonly used data types are integer, floating point, and boolean. When choosing a data type, it is important to consider the range and precision of the values that will be stored, as well as the memory space required. The sizeof()
operator can be used to determine the size of a data type in bytes.