Variables and data types

In the example Input / Output we defined a variable of data type string to store keyboard input and in this chapter I will declare all about variables and data types.

Identifiers

First we must talk about how we define a valid identifier, because a valid identifier can be a sequence of one or more letters, letters with digits like a1 or underscore characters like a_1. Not a part of an identifier sequence are spaces, punctuation marks and symbols. Identifier shall always begin with a letter and can not begin with a digit. You can begin with an underscore character but on most cases such identifier are reserved for compiler specific keywords.

Keywords of C++ are used to identify operations and data descriptions. The standard reserved keywords are:

alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq

Specific compilerst may also have adfitional specific reserved keywords.

Very Important:

The language of C++ is case sensitive that means that an identifier written in capital letters is not the same identifier that written in small letters. To example if you like to define a variable called result and you type in RESULT or Result (or result) then are all different identifier.

Data types

Somewhere in an unspecified location in the computer memory are stored the values of variables as zeros and ones. The program does not need to know the exact memory address where the information of a variable is stored but it need to know what kind of data are stored. It exist different types of data and in many cases they are not interpreted in the same way or they do not occupy the same amount of memory.

Fundamental data types are basic types which represent the basic storage units and they are supported by most systems.

Fundamental datatypes:
GroupType namesNotes on size / precison
Character typecharExactly one byte in size. At least 8 bits.
Character typechar16_tNot smaller than char. At least 16 bits.
Character typechar32_tNot smaller than char16_t. At least 32 bits.
Character typewchar_t (deprecated)Can represent the largest supported character set.
Integer type (signed)signed charExactly one byte in size. At least 8 bits.
Integer type (signed)(signed) short (int)Not smaller than char. At least 16 bits.
Integer type (signed)(signed) intNot smaller than short. At least 16 bits.
Integer type (signed)(signed) long (int)Not smaller than int. At least 32 bits.
Integer type (signed)(signed) long long (int)Not smaller than long. At least 64 bits.
Integer type (unsigned)unsigned charExactly one byte in size. At least 8 bits.
Integer type (unsigned)unsigned short (int)Not smaller than char. At least 16 bits.
Integer type (unsigned)unsigned (int)Not smaller than short. At least 16 bits.
Integer type (unsigned)unsigned long (int)Not smaller than int. At least 32 bits.
Integer type (unsigned)unsigned long long (int)Not smaller than long. At least 64 bits.
Floating-Point typefloatExactly 4 byte in size. At least 32 bits (single precision).
Floating-Point typedoubleNot smaller than float. At least 64 bits (double precision).
Floating-Point typelong doubleNot smaller than double. At least 80 bits (upper precision than double).
Boolean typeboolNo storage.
Void typevoidNo storage.
Null pointerdecltype (nullptr)No storage.

The names of integer datatypes can be abbreviated without their signed and int components. You can write optional to example:

Declaration of variables

All variables have to declared with its type in C++ befor its first use. The compiler needs this declaration to reserve the size in memory for the variable and how to interpret its value. First we need type name and followed of variable name and optionally its initialized with a start value.


Example:
inti = 0;
floatmy_float;

These are two declared datatypes which the first one is an integer datatype called i and initialized start value of zero and the second one is a floating-point datatype called my_float. It is possible to declare more than one variable in one line of the same datatype.


Example:
inta, b, c;

This are three declared variables called a, b, c and all of them of type integer but you can also declare as follows.


Example:
inta;
intb;
intc;

Initialization of variables

It exists three ways to initialize variables in C++ and they are all equivalent.

The first way is a c like intitialization and the variables are initialized with an equal sign and a value.


Example:
intcounter = 0;

A second method to initialize a variable is to initialize with the constructor notation.


Example:
intcounter(0);

The third method known as uniform initialization and is similar to the constructor notation but with curly braces instead of parentheses (revision of the C++ standard, in 2011).


Example:
intcounter{0};

Type deduction: auto and decltype

Is a variable initialized the compiler can figure out what the type of variable is automatically by the initializer. The comand auto is used to figure out the type of given variable to intialize the new variable.


Example:
intfoo = 0;
autobar = foo;

The variable bar is declared with the type auto and has the same data type as foo and is initialized with 0.


Shall the variables are pre-initialized by compiler than it is possible to use type deduction with the decltype specifier.


Example:
intfoo = 0;
decltype(foo)bar;

Introduction to strings

The type string is used for compound characters and the library string has usefully functions to work with strings or to manipulate it.

The difference to fundamental data types is that strings use objects and the library needs to include with this preprocessor directive:
#include <string>


Example:
stringmy_string = "This is a string";
stringmy_string ("This is a string");
stringmy_string {"This is a string"};

In the example above you can see that the initializing of a string has the same syntax as fundamental data types.

Output with string
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6    string my_string = "This is a string";
7    
8    cout << my_string << endl;
9    
10    return 0;
11}