#define ledPin 13 vs int ledPin = 13?

I see that in some sketches people use

#define ledPin 13

as opposed to

int ledPin = 13:

Why would you use one over the other?

Please, not again.

The #define replaces the string with the value at compile time.

The other is a variable that uses um memory space. You should only use that if you want to change that in the code even so then

byte ledPin =13;

Would be better as it only uses 1 byte. However the proper way is

const byte ledPin=13;

This flags up a warning if you try and change it and once compiled does not take up any SRAM memory.

Thanks!

Grumpy_Mike: However the proper way is

const byte ledPin=13;

This flags up a warning if you try and change it and once compiled does not take up any SRAM memory.

Constant variables don't take up RAM space?

Granted, I'm a complete noob at this, but the impression I got was that labeling a variable as constant simply caused the compiler to make sure you don't change it. But that in the compiled code, it was exactly the same.

(And I'd been using the "define" method, for exactly that reason. Not that saving one byte of RAM really makes that much difference, to me.)

trekkie: Constant variables don't take up RAM space?

Granted, I'm a complete noob at this, but the impression I got was that labeling a variable as constant simply caused the compiler to make sure you don't change it. But that in the compiled code, it was exactly the same.

If the Arduino software used C as its language, you would be correct. However, const integers in the C++ language don't take up space unless you take their address.

trekkie: (And I'd been using the "define" method, for exactly that reason. Not that saving one byte of RAM really makes that much difference, to me.)

Well, given the memory sizes of these embedded chips, you might need to sweat over every single byte if your program gets big enough.

This is the most concise recap of this old argument that I've ever seen! I think practice has made perfect.

MichaelMeissner: However, const integers in the C++ language don't take up space unless you take their address.

Which no-one in their right minds would do. ;)

...

Or would they? Not in this context, anyway.

[quote author=Nick Gammon link=topic=202157.msg1492006#msg1492006 date=1386049602]

MichaelMeissner: However, const integers in the C++ language don't take up space unless you take their address.

Which no-one in their right minds would do. ;)

Or would they? Not in this context, anyway. [/quote] You might call a function with a & argument (the & implicitly takes an address), or perhaps it takes a normal pointer to const int, and you do &a. It may be rare to want to take the address of a const int, but I can imagine a use for it.