Variable Types
Learn the different types of data that may be stored in a variable and understand the difference between them.
C++ requires that the data type of each variable be specified before the variable is used. This lesson focuses on the basic data types—also known as primitive data types—that form the fundamental building blocks for storing data within a program. A variable’s data type determines the following:
The type of data that may be stored
The range of values that may be assigned
The operations that may be performed on the data
As we will see shortly, the larger the range of values allowed by the data type, the larger the memory region occupied by the variable. Can we assign the largest possible data type to each variable and simplify the coding process? No, we must not. It would be highly inefficient. C++ programs are expected to execute on physical computers that only have a finite amount of memory. Some environments, such as IoT devices, are especially resource-constrained. Thus, it is important that we use the smallest possible data type big enough to hold the data to ensure an efficient utilization of memory. Remember, you wouldn't buy an airplane hangar to park your motorbike.
Integers
An integer is a number that does not have any decimal places. It is a whole number. For example, 1
, 2
, 3
, 4
, etc., are all integers, but 4.3
is not. There are different integer data types available in C++. These differ in the range of values that they may hold. The following table lists these types, their sizes, and the range of values
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy