Learning C++
- Part 1: Variables and Data Types
- Part 2: Arrays
- Part 3: Functions
- Part 4: Conditional Statements
- Part 5: Looping
- Part 6: Pointers
- Part 7: Objects
- Part 8: Complex Data Types
Variables and Data Types
One of the most frequent metaphors for learning about variables is thinking of them as buckets. Depending on the type and size of the bucket, it can hold different things. The design of the bucket helps in understanding how it can be used.

Like with the design of buckets, variables in C++ have types. Depending on their type, they hold and can be used in different ways.
Integers
One of the most common types in C++ is the Integer. Written as int, it stores whole numbers from 0 up to (usually) 32,767. They are often used with dealing with basic mathematics.
Floats
To store decimals, C++ uses float. This is an abbreviation of the longer compound phrase “floating-point” that describes storing numbers before and after a “point.” In other words, these are decimals that have both whole and fractional values.
Double, Long, and Long Double

C++ also supports three other different common types when working with numbers: double, long, and long double. Like where float stands in for “floating-point,” the use of the keyword double stands in for the phrase “double-precision.”
The Float data type is what is called single-precision. Because C++ comes from a time of very limited memory usage, programmers needed to know if they were dealing with numbers that were relatively small or large, float, or needed more “precision” (space) when dealing with them, double.
For problems with even smaller or larger numbers, the data type long can be used. It is like double, but “bigger.” In fact, the keyword double can be combined to indicate that the number stored will either be very small or very big through using long double.
Characters

When dealing with letters, numbers, or special characters, the char keyword can be used. It represents Characters: a single letter, number, or special character. When used in C++, characters use single quotes around their value.
Booleans

Invented by George Boole, Boolean values are either true or false. In C++, the keyword bool is used. Variables of the type bool can only have the values true or false.
Assignment

When the equal sign (=) is used with a variable, it is assigned a value. Like with the metaphor of a bucket, different values can be “put” into a bucket based on its type. This is the same for variables. When a value is “put” into a variable, it is assigned that value.
Trying to “put” a value into a variable it was not designed to hold can often, but not always, result in an error. This is why knowing the type of a variable is important: types and what kinds of values are tightly connected. Only variables of certain types can hold certain values.