A new episode on our Arduino IDE series, dealing with the correct declaration of variables, constants and macros for our source code.
The “move” of a problem to the computer system is indicated in the computer language with the term implementation.
We talk of implementing a problem when the solution is addressed through the construction of a model.
For example, weather forecasts require the availability of data regarding temperature and pressure measured in many locations. The target place of forecasts, physical quantities and measured values constitute the entities.
Each entity has one or more properties. For example, the properties of a place are: name, latitude, height above sea level and so on. As far as the measurement is concerned, the properties can be the following: time of detection and values of each measurement for a given physical quantity.
These properties can take different values for different measurements, and then they behave like variables. Variables are properties of entities that can take different values for different samples of the same entity. The use of variables is common practice in Math.
As the name “variable” implies, the value attributed to them can change, and each update of the value of a variable corresponds to a change in the state of the program.
A variable is similar to a box that can contain only one object (data) at a time. Each variable has a unique name that distinguishes it from all the others.
The variables used in a program must be first declared: at the beginning of the program or before their use, indicating the type and name assigned to the variable. The declaration of a variable must contain the type associated with it, as the following syntax shows:
The name to identify a variable (or function or constant) is commonly referred to as an identifier. In C / C++ the identifier must start with an alphabetic character or an underscore.
Technically,the identifier of a variable is a label that addresses a unique memory location on the computer hardware.
The following table shows the data types of the programming language used by the Arduino IDE:
|Caratteri/Characters||char||da -128 a 127||8|
|Interi/Integers||Short int||da -32.768 a 32.767||16|
|int||da -2.147.483.648 a 2.147.483.647||32|
|Reali/Real||Float||da 1,17549435 * 10^-38 a 3,40282347 * 10^38||32|
|double||da 2,2250738585072014 * 10^-308 a 1,7976931348623157 * 10^308||64|
|long double||da 3,4 * 10^-4932 a 1,1*10?4932||80|
Examples of variable declarations:
- int number;
- int year;
- float height;
- double Area;
- char reply;
- bool found;
Following the declaration, the variable must be initialized, i.e. it must be assigned a value, an operation that can be done at the same time as the declaration.
The = symbol is used as assignment, used to associate a value with a given variable. The assigned value can be a constant, another variable, the result of an expression or the return value of a function and so on.
- int number = 10;
- int year = 2020;
- char answer = ‘y’;
- bool found = ‘true’;
Multiple variables declared with the same name cannot exist.
In solving a problem it is often possible to identify properties that always take the same values and don’t change during the execution. These properties are called constants.
A variable is constant when it does not change its value during program execution.
The constants used in a program are declared through the const keyword according to the following syntax:
const type name = value;
The use of constants ensures that the value assigned before the execution of the code is kept unchanged throughout the execution of the program, avoiding unwanted changes.
In other word a const variable is assigned once and is read-only.
- const float PI;
- const float E;
- const int YEAR;
As a rule, the identifier of a constant uses capital letters.
There is another way to define a constant value over time. The #define preprocessor directive.
A preprocessor directive is an action taken before the compilation of a program. The directive makes a substitution between the literal and the value.
The directive is used to define a MACRO, or a symbol. The syntax with which to use the directive is as follows:
#define MACRO_NAME macro_value
Note the absence of the ‘=’ sign and of the semicolon at the end of the declaration: a preprocessor directive IS NOT a C / C++ statement.
The construct #define creates a macro. No variables are declared but a macro is set through it. Before execution, the compiler will associate the “macro_value” to “macro_name”.
Before execution, the compiler will exchange the word ledpin with the value 3 on the source code. Here is the code after the substitution:
What is the difference with const? The #define macro replaces the value in the code before compilation, therefore it does not need a memory location, while the const variable does.
Generally the execution of a macro is faster than the use of a const variable, but it is also subject to side effects due to an incorrect evaluation of the expressions by the preprocessor (the system that takes care of translating the macros before of compilation).
We will talk about it in a future article.
Previous articles on the same subject:
Follow us to keep yourself updated with the news!