Numerical Data Types
Whole numbers
Integer data types are the most commonly used for whole numbers. The int data type can hold values within the range of -2,147,483,648 to 2,147,483,647.
main.cpp
12345678910#include <iostream> int main() { int goodNumber = 12; int tooLarge = 2147483648; std::cout << "Printing goodNumber: " << goodNumber << std::endl; std::cout << "Printing tooLarge: " << tooLarge << std::endl; }
That happens because when you declare an int variable, it allocates precisely 4 bytes of your PC's memory. And numbers above 2147483647 (or below -2147483648) do not fit inside those 4 bytes. Luckily, other data types available can allocate you more(or less) space for your needs. Here is the table:
So you can use long to store large numbers (e.g., World's population). You can also use short if you are sure your number will not exceed the -32,768 to 32,767 range (e.g., storing a user's age). Using short will take up less memory space.
main.cpp
12345678910111213#include <iostream> int main() { short age = 22; // for small numbers int likes = 143200; // for larger numbers long population = 8200000000; // for very large numbers // Display the values std::cout << "Age: " << age << std::endl; std::cout << "Likes: " << likes << std::endl; std::cout << "World's population: " << population << std::endl; }
Be careful with the data type you select. If the range of a type is exceeded, the C++ compiler will not notify you, and you may receive an unexpected value without any indication of an error.
Floating point numbers
The data types above are designed to store whole numbers. If we tried to assign 1.6 to one of those, here is what we would get:
main.cpp
12345678#include <iostream> int main() { // Change the data type to `float` o `double` int num = 1.6; std::cout << num << std::endl; }
The int type truncates the decimal part of a number. This behavior is the same for short and long types. To store floating-point (decimal) numbers**, you should use either the float or double data type.
Here is an example of using double to store 1.6.
main.cpp
12345678910#include <iostream> int main() { float floatNum = 123.45678; double doubleNum = 123.45678; std::cout << "using float:" << floatNum - 123 << std::endl; std::cout << "using double:" << doubleNum - 123 << std::endl; }
Since the float type only has a precision of seven digits, the number 123.456789 is already out of its range. It can lead to inaccurate results(as shown in the example below). So it is better to use double by default unless you are sure float's precision is enough.
Obviously, you can use float or double to store whole numbers since those are decimal numbers with a decimal part equal to 0. However, as a good practice, if a variable stores values that can only be whole numbers (e.g., population or likes), short/int/long should be used.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Numerical Data Types
Swipe to show menu
Whole numbers
Integer data types are the most commonly used for whole numbers. The int data type can hold values within the range of -2,147,483,648 to 2,147,483,647.
main.cpp
12345678910#include <iostream> int main() { int goodNumber = 12; int tooLarge = 2147483648; std::cout << "Printing goodNumber: " << goodNumber << std::endl; std::cout << "Printing tooLarge: " << tooLarge << std::endl; }
That happens because when you declare an int variable, it allocates precisely 4 bytes of your PC's memory. And numbers above 2147483647 (or below -2147483648) do not fit inside those 4 bytes. Luckily, other data types available can allocate you more(or less) space for your needs. Here is the table:
So you can use long to store large numbers (e.g., World's population). You can also use short if you are sure your number will not exceed the -32,768 to 32,767 range (e.g., storing a user's age). Using short will take up less memory space.
main.cpp
12345678910111213#include <iostream> int main() { short age = 22; // for small numbers int likes = 143200; // for larger numbers long population = 8200000000; // for very large numbers // Display the values std::cout << "Age: " << age << std::endl; std::cout << "Likes: " << likes << std::endl; std::cout << "World's population: " << population << std::endl; }
Be careful with the data type you select. If the range of a type is exceeded, the C++ compiler will not notify you, and you may receive an unexpected value without any indication of an error.
Floating point numbers
The data types above are designed to store whole numbers. If we tried to assign 1.6 to one of those, here is what we would get:
main.cpp
12345678#include <iostream> int main() { // Change the data type to `float` o `double` int num = 1.6; std::cout << num << std::endl; }
The int type truncates the decimal part of a number. This behavior is the same for short and long types. To store floating-point (decimal) numbers**, you should use either the float or double data type.
Here is an example of using double to store 1.6.
main.cpp
12345678910#include <iostream> int main() { float floatNum = 123.45678; double doubleNum = 123.45678; std::cout << "using float:" << floatNum - 123 << std::endl; std::cout << "using double:" << doubleNum - 123 << std::endl; }
Since the float type only has a precision of seven digits, the number 123.456789 is already out of its range. It can lead to inaccurate results(as shown in the example below). So it is better to use double by default unless you are sure float's precision is enough.
Obviously, you can use float or double to store whole numbers since those are decimal numbers with a decimal part equal to 0. However, as a good practice, if a variable stores values that can only be whole numbers (e.g., population or likes), short/int/long should be used.
Thanks for your feedback!