Go Down

Topic: Very quick question about byte and int. (Read 536 times) previous topic - next topic

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:

Code: [Select]
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
There are three kind of people in the world: Those who can count, and those who can't

James C4S


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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

PaulS

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

Krupski


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!)  :)
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

fungus


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.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

James C4S

#5
Nov 22, 2012, 06:15 pm Last Edit: Nov 22, 2012, 06:20 pm by James C4S Reason: 1

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:
http://arduino.cc/forum/index.php/topic,86800.msg651320.html#msg651320
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

WizenedEE



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

Go Up