Very quick question about byte and int.

I noticed that a lot of example sketches that I download from here or elsewhere use int type constants and variables instead of byte even when byte would be enough:

const int LED = 13 ;  // 13 is < 256, byte would be enough

With an 8 bit MCU I would think using byte would be better (faster and half the memory) than int.

My question is: is there a drawback in using byte rather than int or, putting it in another way, an advantage using int instead of byte when byte is enough?

TIA

Thot:
With an 8 bit MCU I would think using byte would be better

better is subjective

I suspect "int" is used because new programmers more easily associate numbers with integers.

Using byte is generally better. Typing byte, instead of int, requires 33% more effort.

PaulS:
Using byte is generally better. Typing byte, instead of int, requires 33% more effort.

And using #define led 13 uses zero bytes (but lots more effort!) :slight_smile:

Thot:
My question is: is there a drawback in using byte rather than int or, putting it in another way, an advantage using int instead of byte when byte is enough?

Code can be much smaller/faster with bytes.

Krupski:
And using #define led 13 uses zero bytes.

In this case, so does the const keyword.

Edit: Found it. There's a thread floating around where a forum member did some experiments and showed that const also consumes no RAM.

http://arduino.cc/forum/index.php/topic,86800.0.html
In particular, Nick's example:

Krupski:

PaulS:
Using byte is generally better. Typing byte, instead of int, requires 33% more effort.

And using #define led 13 uses zero bytes.

const byte, const int, and #define should all use zero bytes unless you don't use any optimizations or take the address of the constant.

int is a standard C++ type and byte isn't, so int is more portable' and recognizable to people not used to arduino. uint8_t is more standard but more annoying to type, and in C and C++ you have to include a header file to use it (stdint.h). char is undefined as to whether it's signed or unsigned so if you care you have to put ``unsigned' in front of it which is a whole lot more effort.

There's no real good solution. I generally stick with uint8_t in libraries, but I think byte is best for sketches