Loading...
Pages: [1]   Go Down
Author Topic: Very quick question about byte and int.  (Read 221 times)
0 Members and 1 Guest are viewing this topic.
Maryland, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
 
Logged

There are three kind of people in the world: Those who can count, and those who can't

Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5176
CMiYC
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

www.cmiyc.com - A guide to being an Enginerd

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 316
Posts: 35593
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 15
Posts: 463
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!)  smiley
Logged

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2282
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5176
CMiYC
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 22, 2012, 12:20:11 pm by James C4S » Logged

www.cmiyc.com - A guide to being an Enginerd

Offline Offline
Edison Member
*
Karma: 15
Posts: 1009
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Print
 
Jump to: