About the "int" data type variable

I was looking at some example code and noticed these lines:

// constants won't change. Used here to 
// set pin numbers:
 const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

My question is, if the "int" type stores integers, why is it being used to store "LOW?" Why would this not make a compiler error?

Later in the program they use it like this:

  if(currentMillis - previousMillis > interval) {
     // save the last time you blinked the LED 
     previousMillis = currentMillis;   

     // if the LED is off turn it on and vice-versa:
     if (ledState == LOW)
       ledState = HIGH;
     else
       ledState = LOW;

     // set the LED with the ledState of the variable:
     digitalWrite(ledPin, ledState);

So my question is, what is it about the int variable that lets you store "LOW" or "HIGH" in it, and not a number?

Second question is wouldn't the program be better (more memory efficient) to change ledState and do, for example:

boolean ledOn=true;

Thanks!

My question is, if the "int" type stores integers, why is it being used to store "LOW?" Why would this not make a compiler error?

Because LOW is defined to be replaced with zero.

You would be better storing it in a "byte" (uint8_t) variable.

What you see:

digitalWrite (pin, LOW);

What the compiler sees:

digitalWrite (pin, 0);

So my question is, what is it about the int variable that lets you store "LOW" or "HIGH" in it, and not a number?

You have the question the wrong way round. HIGH and LOW (no quote marks) are defined as numbers behind the scenes so that they can be stored in an int. Even better because it saves space, they can be saved in a byte if you want to. A boolean takes a byte of space to store true or false so would also save space compared with an int. Better still it makes the program more readable if good variable names, such as boolean ledOn = true;and you can also shorten tests as in

if (ledOn)
{
  //do stuff if the led is on
}

Yes, bool, or byte, would be more efficient. Either takes 1 byte, vs 2 bytes for an int. I generally use byte ledPin = 13; others use const byte ledPin = 13; and also #define ledPin 13

and then byte flag = 0; HIGH and LOW are defined elsewhere as 1 and 0. So and then check: if (flag ==1){ }, or if (flag == 0){ } does the same as if (flag ==HIGH){ }, also if (flag == true){ } (or maybe TRUE? one or other, 0 and 1 is more clear)

Any time related elements should be unsigned long, as that is what millis( ) and micros( ) return.

UKHeliBob: You have the question the wrong way round. HIGH and LOW (no quote marks) are defined as numbers behind the scenes so that they can be stored in an int.

OK this makes perfect sense now.

The only other thing that interests me is that both a boolean and byte variable take up 1 byte of RAM. I would have thought that a boolean would only use one BIT, as there are only two states for that -- true or false.

I realize this is getting too computer science-y, and more technical than I need to be at my level, so I'll leave it at that. Thanks for the help to everyone that replied.

A bool could be one bit, but it would take too long to access it, so the compiler makes it a byte for speed reasons.

C does allow for single-bit variables: check out bitfields.

C does not allow single bit data types; if you can't form a pointer to it, it can't be a variable. Bit fields are a poor cheat - you can't have an array of bits.

I didn't say use a bitfield, I said check it out. 8^)

And I am sure a compiler could work things to have an addressable single bit variable.

From the C programming FAQ:

"Some 64 bit Cray machines represent int * in the lower 48 bits of a word; char * additionally uses the upper 16 bits to indicate a byte address within a word."