user50887: I'm new to Arduino and embedded, but understand that it is often better to use global instead of local variables (such as here or here).
I have this simple code (from here):
//Libraries
#include <DHT.h>;

//Constants
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    Serial.begin(9600);
    dht.begin();

}

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}

My ancient C programmer training (Unix servers) says I must move variables hum and temp to loop(), or is it better on Arduino to leave them as globals?

user37622: The general rule of good practice is to minimize the scope of a variable to necessary minimum. Write the code to be readable and maintainable, don't do premature optimization.
The variables hum and temp should be defined only in loop(). The compiler can optimize it by putting them to global space or using MCU registers only, to avoid repeated creation on stack. 

user7508: No. If you have the choice, locals are usually better, because they
minimize the risk of name collision, and they help make the program
clearer by keeping the variable definition close to the place where it
is used. In your example, hum and temp should be locals, as there is
no good reason to make them globals.
However, sometimes you don't have the choice. If a variable is used in
both setup() and loop(), then it needs to be global. This is the
case of dht in your example.
Sometimes a variable is used in many functions. Even if you can make it
local, by passing it around through function parameters, doing so
doesn't make the program more readable. In that case you may also prefer
to keep it global.
Sometimes a variable that is used in only one function needs to be
statically allocated in order to preserve its value across calls to the
function. In this case the variable could be a static local. However,
since the static keyword is somewhat obscure for beginner programmers,
Arduino tutorials tend to use globals whenever static storage is needed.
You are not required to follow this practice, although you may want to
if you are writing for an audience of novices.
In any case, the take away of your first two links is not that you
should prefer globals: it's that sometimes there is a good reason for
making a variable global, and that's fine.

50887: <p>I'm new to Arduino and embedded, but understand that it is often better to use global instead of local variables (such as <a href="https://arduino.stackexchange.com/questions/41493/are-global-variables-evil-in-arduino">here</a> or <a href="https://arduino.stackexchange.com/questions/29113/avoid-global-variables-with-classes">here</a>).</p>

<p>I have this simple code (from <a href="https://www.instructables.com/id/How-to-use-DHT-22-sensor-Arduino-Tutorial/" rel="nofollow noreferrer">here</a>):</p>

<pre><code>//Libraries
#include &lt;DHT.h&gt;;

//Constants
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    Serial.begin(9600);
    dht.begin();

}

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}
</code></pre>

<p>My ancient C programmer training (Unix servers) says I must move variables hum and temp to loop(), or is it better on Arduino to leave them as globals?</p>
 37622: <p>The general rule of good practice is to minimize the scope of a variable to necessary minimum. Write the code to be readable and maintainable, don't do <a href="http://wiki.c2.com/?PrematureOptimization" rel="nofollow noreferrer">premature optimization</a>.</p>

<p>The variables hum and temp should be defined only in loop(). The compiler can optimize it by putting them to global space or using MCU registers only, to avoid repeated creation on stack. </p>
 7508: <p>No. If you have the choice, locals are usually better, because they
minimize the risk of name collision, and they help make the program
clearer by keeping the variable definition close to the place where it
is used. In your example, <code>hum</code> and <code>temp</code> should be locals, as there is
no good reason to make them globals.</p>

<p>However, sometimes you don't have the choice. If a variable is used in
both <code>setup()</code> and <code>loop()</code>, then it needs to be global. This is the
case of <code>dht</code> in your example.</p>

<p>Sometimes a variable is used in many functions. Even if you can make it
local, by passing it around through function parameters, doing so
doesn't make the program more readable. In that case you may also prefer
to keep it global.</p>

<p>Sometimes a variable that is used in only one function needs to be
statically allocated in order to preserve its value across calls to the
function. In this case the variable could be a <code>static</code> local. However,
since the <code>static</code> keyword is somewhat obscure for beginner programmers,
Arduino tutorials tend to use globals whenever static storage is needed.
You are not required to follow this practice, although you may want to
if you are writing for an audience of novices.</p>

<p>In any case, the take away of your first two links is <em>not</em> that you
should prefer globals: it's that sometimes there is a good reason for
making a variable global, and <em>that's fine</em>.</p>
