Whenever you write or use a program, you handle data. If you fire a gun in a first-person shooter game, the movement of your bullet is calculated using data about its initial position. What you see on the screen is the result of thousands of consecutive operations performed on this data by manipulating the values in your computer’s memory. As a programmer, you use variables to name the data, use it, and reuse it.
In C++, a variable always has an associated data type, depending on that variable’s nature and purpose. A variable can contain a small or a large number, a string of text, or a more complex user-defined structure. This is where data types come into play.
But what exactly are data types and how do they work in C++? This article tells you everything you need to know about data types and why they’re so important in C++.
What Is a Data Type?
A data type in the C++ programming language is an attribute that defines a value’s nature. In the computer’s memory, this results in a certain amount of space dedicated to the value. Memory is limited and therefore needs to be used effectively, as much as possible.
Let’s say you manage a zoo, and you care for animals of different sizes. If you keep spiders in the elephant area, the spiders will definitely appreciate it, but you may run out of space to keep your elephants. But if you place the spiders into their own, compact area, they’ll still be fine and the elephants will also have enough space to roam. Likewise, in C++, it is important to store the data in the right place to avoid memory waste.
In the world of C++, there’s an ISO standard that defines the minimum size for each data type, in bytes. The compiler in C++ translates C++ code into low-level code that’s understandable directly by the computer. During the compilation process, the compiler figures out each variable’s type from the code and understands how much memory will be required for each variable.
To decide which data type to use, you first need to be aware of your options. Let’s go through the most common C++ data types and show you how to use them.
The 5 Most Common Data Types
The integer type is one of the simplest and most popular data types. The C++ standard defines the minimum size of an integer at four bytes, but different compilers can use larger sizes.
|Data type||Value range|
|Signed Integer(default)||-2147483648 to 2147483647|
|Unsigned Integer||0 to 4294967295|
Here’s an example of how to use the int type in C++:
2 billion is a big number. If you need a variable to store “human size” data, such as a number of siblings or a person’s age, using an integer will most likely cover your needs. If you only need positive numbers, you can easily declare an unsigned Integer using this syntax:
The image below shows you how the value of an integer variable is stored in the memory:
As you can see, when the integer is four bytes long, storing it will always occupy four bytes in the memory, regardless of the value of the number it represents. This is the case for all basic data types — storing a “0” does not take up less space than storing “14738.” It’s thus crucial to choose the right data type.
As a programmer, you may find yourself manipulating numbers with fractional parts, such as an angle (e.g., 36.56°). These are called floating point numbers or real numbers, meaning they have a decimal point (e.g., 1.234 or 67.58). An Integer variable cannot help you here because the compiler ignores the numbers after the point. This is where the float data type comes into play.
Typically coded in four bytes in C++, the float allows you to store a number with a fractional precision of seven digits with a floating-point.
Note: Only seven significant digits can be recorded in a float type, in addition to the exponent. This limits the precision to a maximum of six digits after the comma, according to the IEEE 754 standard that the C++ standard references.
If you feel a bit restricted using float and you need to do more advanced operations, you can use the double type. Similar to float but using eight bytes, a double can store up to 16 significant digits after the decimal point. If you need to store the square root of a number, the result is likely to have many decimals. In such cases, doubles are highly useful.
Let’s imagine you see an ad for a 354 ft2 room. To find out the length of the room’s walls, simply use this code:
Sometimes, you’ll need to store facts that can be true or false. For example, you may want to record whether a particular person has a driver’s license. In C++, the best practice for this is to use the
Any non-zero number, when converted into a boolean value, is equivalent to “true”, and a zero is equivalent to “false”. The example below illustrates the use of booleans in context. To win, a person has to be assigned a number lower than 50. The program will assign this number randomly.
It’s worth noting that if any value other than 0 is assigned to a boolean variable, it acts as if the value were 1, which means “true.” However, the compiler might generate a warning if anything other than a “true” or a ”false” (1 or 0, respectively) were to be compared to a variable of type bool. For more information about logical operators in C++, check out our article on the topic.
Forstoring a single characterthe char data type is your choice. Here’s how you could display the value of a char variable:
char is the perfect type for storing and working on a character. In the computer’s memory, characters are stored as numbers. The numbers corresponding to characters are shown in the ASCII table. ASCII characters usually take up one byte of memory, but extended Unicode encodings like UTF16 may require more.
If you need to store more than one character — perhaps someone’s name or a complete address — arrays of
char values are used in C but are not best practice in C++. where programmers use the
string data type.
The string is a bit special, as it’s not a “primitive” C++ data type. It’s not part of the language’s foundation. However, the
string library is included in the standard library of most modern compilers and its use is transparent and similar to the other types:
How To Determine the Size of a Data Type in C++ ?
As mentioned above, compilers can assign more memory to the data types than the minimum required by the ISO standard. To know the size of a data type, you can use the
sizeof() operator. It will give you the result in bytes. In Microsoft Visual Studio, the size of
double is 8 bytes.
There are more data types available in C++. In this article, we covered those that you absolutely need to know to start programming. You can find the comprehensive list of built-in types here. Keep in mind that more complex programs usually have their own custom types and classes that can have varying sizes.
Learn C++ Online
Data types are fundamental to C++ programming, and you’ll need to know them well to write efficient and powerful programs.
Thinking of becoming a C++ developer? There’s never been a better time!
C++ skills are in high demand in robotics, gaming, healthcare and many other industries. Udacity’s specialized C++ Nanodegree program is designed to prepare you for a career in these high-stakes settings.
Enroll in our C++ Nanodegree program today!