#define vs variables?


i'm positive must have been discussed somewhere before, searches didn't turn up, apologies.

i wondering why seems convention here use integers define pin mappings. wouldn't #define more space efficient, since pins presumably aren't changing? and, if there reason using variable, why not use smaller one, byte, rather int?

that said, in general, why should 1 use #define on variable, , vice versa?

thanks indulging newbie!  :)

good questions.

using #define little more efficient.
in blink example sketch, replacing:
  int ledpin = 13;
with:
 #define ledpin 13

actually reduces code size  8 bytes.

but when need manipulate value variable (i.e. looping through pins in loop) declaring variable byte little more efficient int.

i think int used in examples newbies don't confused seeing types not yet familiar with.

there many factors take consideration when choosing #defines on variables. pre-processor can optimize constants @ compile time use them when can. pre-processor not understand c language behavior of code can different seemingly similar variable expression. also, #defines can little difficult debug using variable no bad thing unless efficiency priority.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > #define vs variables?


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums