Hello and welcome. Have you once tried to justify in one sentence and few words.

“What do I actually program for?”

Surely you can think of many answers to this question. Some are a bit longer, others describe a specific use case. But basically, every program serves the same basic purpose:

processing data.

It doesn’t matter if it’s numeric, alphabetic, audio or video data. You probably know many more types of data. But no matter which, you basically just want to put it into a form you want.

Take a simple math problem to illustrate. In this example, your data are the two numbers 4 and 6, which are added to a sum.

// listing1: example equation

4 + 6 = 10

But how does a computer do this job?

Well, first you have to keep in mind that a computer consists of a computing unit and the memory. The computing unit can be a CPU or a small microprocessor that performs the data processing. It takes the data from the memory. This can be either in volatile RAM or persistent in ROM.

The processing unit takes the binary code of the program and the required data from the memory and executes the task.

And what if I want to calculate the same arithmetic task with other numbers?

Variables

Now variables come into play. Just like in mathematics, instructions can be defined as a function with variables.

If you’re thinking to yourself now “Oh, my God, now he’s going to start with math…”, then I’m sorry to tell you that you won’t get past this. After all, the devices are called calculators or computational units for a reason.

Variables are placeholders. You describe your function without knowing which data should be changed later. You also want to store the result of your function briefly and use it later. Therefore you set a variable and reserve a place for an unknown value in the memory. When you start your program, the value is calculated during runtime and stored in the variable.

The variable now holds a value, but you can always assign new values to it. So the value is only temporarily available. Until you reuse the variable for other data.

You can determine the name of the variable yourself.

// listing2: equation with variables

Variable1 + Variable2 = Result

However, you are not completely free with the naming. C++ forbids some things and punishes them immediately with compile errors. Variables may contain numbers and letters, but they may not start with a number. Also arithmetic operators (+, -, etc.) are not allowed. As soon as you put a space, the compiler interprets the end of the name. There are also reserved keywords that you are not allowed to use. One of them you already know: return.

Choose meaningful names for your variables. This makes your code easier to understand and maintain.

The syntax of C++ tells you how to declare variables. First you specify the data type and then the identifier, the name of your variable.

// listing3: definition of a Variable

float radius = 3.0; // datatype identifier = value;

Constants

Variables therefore hold values only temporarily and can be overwritten at any time. But in some cases this is not desired. Imagine you want to determine the area or circumference of a circle.

// listing4: circle formulas

Area = pi * Radius * Radius;
Circumference = 2 * pi * Radius;

No matter which circle is involved, the value of pi remains the same. pi is a constant in mathematics and has a fixed value. So you never want to change the value and C++ helps you not to accidentally overwrite the placeholder of pi with a wrong value. You have the option of giving these variables the properties of constants.

The definition in C++ is very similar to that of variables. The only difference is the prefixed keyword const.

// listing5: definition of a constant

const float pi = 3.14159265359; // const datatype identifier = value;

After you define a variable as a constant with a fixed value, you can no longer make changes during program runtime. Assignments to a constant will cause errors during compilation.

Literals

No problem up to this point. There are variables, whose content can be changed, and there are constants, which are unchangeable during runtime.

To this we now add the literals. Literals are fixed values with a certain notation in the source code. Unlike variables or constants, they don’t have an identifier, they are just their value. Sounds very cryptic. Therefore an example.

// listing6: literals

int aNumber = 2;
int aHexValue = 0x10;
char aCharacter = 'C';

In listing6 you see three variables, each of which is assigned a literal in the notation of its data type. 2 is a fixed value without identifier in the source code, i.e. a literal. aNumber is the variable that represents it when called.

The other two lines are to be understood in the same way. 0x10 is a hexadecimal number and 'C' is the character C. I only chose three different literals to illustrate the different notations. There are others, but I think the concept of literals is understandable with this.

Difference between declaration and definition

You have surely already come across the terms declaration and definition. At the latest in this blog entry you have read them. Often the two are confused with each other. But the distinction is not that difficult.

The declaration consists of two specifications: datatype and identifier. The identifier is the name of a variable, object or function. It is presented to the compiler with the description of the data type when declaring.

Here are a few examples of declarations.

// listing7: declarations

int aNumber;                        // variable with integer datatype
double aFunction();                 // function with double return value
double aMethod(int i, double d);    // function with arguments

Now pay attention! What happens now with a definition? Here the identifier is initialized. What does that mean? So far you have reserved a place in memory with the declaration and provided it with names as well as the data type that should be held there.

When you initialize, you define what the linker should reference to these entities, also called entities.

A difficult sentence, but with the extension of the previous example from a declaration to a definition in listing8, you’ll understand.

// listing8: definitions

int aNumber {6};    // initialization

double aFunction()  // definition
{
    return -3.5;
};

double aMethod(int i, double d) // definition
{
    return i + d;
};

You can also replace the declaration directly with a definition. But you always have to define and that exactly only once per declaration! If you forget a definition, the linker will not know which reference belongs to the declaration and will complain with an error message. If you define a thing more than once, the linker won’t be able to decide which reference to take, and will complain about symbols being used twice.

int aNumber; is the definition of aNumber. Although the notation is identical to the declaration. In the C++ standard the definition is defined as follows:

A declaration is a definition unless it declares a function without specifying the function’s body (8.4)

§3.1/1 ISO International Standard ISO/IEC 14882:2014

int aNumber {6}; is the initialization of the variable. It is passed a first value directly at the declaration. With all following value transfers to the variable one speaks of an assignment.

That sticks

Today we have dealt with variables and constants. You now know how to declare and define variables in C++. With the keyword const you make variables immutable. And you know the difference between a declaration and a definition.

You also know that a variable is a placeholder with a name for data in memory. We will look at how much space is reserved in the next blog entry. There we will deal with data types in more detail.

I wish you maximum success!


Sources

  • [1] B. Stroustrup, A Tour of C++. Pearson Education, 2. Auflage, 29. Juni 2018.
  • [2] B. Stroustrup, Programming: Principles and Practice Using C++. Addison Wesley, 2. Auflage, 15. Mai 2014.
  • [3] U. Breymann, Der C++ Programmierer. C++ lernen – professionell anwenden – Lösungen nutzen. Aktuell zu C++17. München: Carl Hanser Verlag GmbH & Co. KG; 5. Auflage, 6. November 2017