problem with assigning global variable values at declaration

I compiled the example below for arduino nano and found that the variable assignment outside the setup doesn't work as intended.
Following the tutorial at https://www.arduino.cc/en/Tutorial/Variables I though I was perfectly fine.
Playing around with different values though showed me that it only works properly when I assign the values within setup().
Is this a known problem or am I doing something wrong here?

unsigned int greenLED = 0 ;
bool         redLED   = 1 ;

void setup()
{   
    //redLED    = 1 ;
    //redLED    = 0 ;
     pinMode( 12, OUTPUT ) ;
     pinMode( 13, OUTPUT ) ;
     digitalWrite( 12, greenLED ) ;
     digitalWrite( 13, redLED ) ;
     while( 1 ) {} 
}
void loop()
{   ;
}

What do you mean by "it only works properly" ?

it only works properly when I assign the values within setup().

And your evidence ?

unsigned int greenLED = 0 ;
bool         redLED   = 1 ;

void setup()
{
  Serial.begin(115200);
  Serial.println(greenLED);
  Serial.println(redLED);
}
void loop()
{
}

Works OK for me.

Note that a boolean variable should only have a value of true or false. Other values may work but your mileage may vary.

This does not look right:

     digitalWrite( 12, greenLED ) ;
     digitalWrite( 13, redLED ) ;

digitalWrite takes a pin number and a parameter of HIGH or LOW, not greenLED or redLED.

It may work or you may get unexpected results.

not greenLED or redLED.

But, if those (stupidly named) variables contain HIGH or LOW, they ARE allowed.

Actually, even if they don't contain HIGH or LOW they are allowed.

UKHeliBob:
Note that a boolean variable should only have a value of true or false. Other values may work but your mileage may vary.

No, it may not vary. The standard tells precisely what your mileage will be.
However, using true or false is recommended and good style.

Which standard? If you can provide a link, I will read it myself, else a quote.

I'm self-taught and have never cared about the standards but I'm curious now.

UKHeliBob:
Note that a boolean variable should only have a value of true or false. Other values may work but your mileage may vary.

You can do this:

bool myBool = 7;

and myBool will have a value of true. Any non-zero value assigned to a bool variable is converted to true. This is guaranteed by the C and C++ standards.

See n1570 (final draft of C11; C++ standards have similar language):

6.3.1.2 Boolean type
1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

And what are their value if you don't assign than in setup()?
Everyways if you want like this means that yiu want const variables (that can conteins, for example, pins or Serialbcomands, or delays). If so you can use #define. It simply change the first word wirch all the following things, so the program recive a number directly

//how to use #define
#define variablename value
//NO =, NO ;

You can do this:

bool myBool = 7;

and myBool will have a value of true.

No. It will have a value of 7. That value will be interpreted as true, because it is non-zero, if used as a boolean.

if(myBool)
{
   // This is true
}
if(myBool == 7)
{
   // Hey, this is true, too
}

No. It will have a value of 7. That value will be interpreted as true, because it is non-zero, if used as a boolean.

So what should this print ?

void setup()
{
  Serial.begin(115200);
  boolean x = false;
  Serial.println(x);
  x = 7;
  Serial.println(x);
}

void loop()
{
}

Adding "while( 1 ) {}" as the last line in "setup()" is probably not what you want.

When you use an instruction, look it up , or look at the examples in the IDE. You can’t “not care “‘about syntax and usage and then wonder why something doesn’t work . The use of standardisation also means others can understand your efforts and offer advice.

PaulS:
No. It will have a value of 7. That value will be interpreted as true, because it is non-zero, if used as a boolean.

if(myBool)

{
  // This is true
}
if(myBool == 7)
{
  // Hey, this is true, too
}

Nope, a bool cannot hold any value other than 0 or 1 (false or true). Read the link to the C11 standard I posted above.

Edit to add: In your example above, the value of myBool will be converted to int before it's compared with 7 (because int is "wider" than bool). Then it's a comparison of 1 == 7, which is false.

For argument,

bool myBool = 7;
printf("%d\n", myBool);
if(myBool == 7) printf("Good\n");
if(myBool != 7) printf("Not\n");

prints

1
Not

I apologize for the confusion.
The assignment of values at declaration time is perfectly fine.
The problem was a loose contact on my breadboard which made the LED output behavior spurious and led me to the the wrong conclusion.

Yup. The compiler assigns 1 to a bool for any non-zero value on the right hand side of an expression.

Also in hardware/tools/avr/lib/gcc/avr/4.9.2/plugin/include/system.h:

# define bool unsigned char

boolrules:
Yup. The compiler assigns 1 to a bool for any non-zero value on the right hand side of an expression.

Also in hardware/tools/avr/lib/gcc/avr/4.9.2/plugin/include/system.h:

# define bool unsigned char

What version of the Arduino IDE is that found in? That's not the standard definition of "bool". I have 1.8.5 and there is no file named "system.h". In hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdbool.h you'll find this line:

#define bool   _Bool

That's for C. For C++ "bool" is already defined by the language without requiring stdbool.h. (_Bool is defined by the language in C99 and later; bool is a synonym when stdbool.h is included.)

1.8.3

boolrules:
1.8.3

Hmm, it's not in a fresh 1.8.3 either (I downloaded the standalone version so I don't have to install it). There's not even a "plugin" directory. Is this file from a core like the ESP2866 perhaps?

After a little investigation, that system.h file seems to be used by GCC to build itself. It's not used by applications that are built by GCC (such as Arduino sketches).

Here's a comment in the code preceding all of the Boolean macro definitions in system.h:

/* Provide a fake boolean type.  We make no attempt to use the
   C99 _Bool, as it may not be available in the bootstrap compiler,
   and even if it is, it is liable to be buggy.
   This must be after all inclusion of system headers, as some of
   them will mess us up.  */