When to use int, const int or define

Hello, I want to know, when we should use int, const int or define

I have seen some codes and I'm not sure when to use each of them when you define a pin output or input

int ledPin = 13; // the number of the LED pin

const int ledPin = 13; // the number of the LED pin

#define ledPin 13 // the number of the LED pin

Is define the best option?

And the point on setting a pin as an int, is to be able to change it at some point in my code?

Thanks you all :slight_smile:

The only one not to use is the naked int. This uses up precious RAM space.

const int or #define should be fine.

1 Like

#define takes no actual RAM, any other "int" like definition does. If you were planning to use "const", then #define will probably work.

You use "int" if you want to store a value between -32768 and 32767 that you want to be able to change. This uses 2 bytes of RAM.

You use "const int" if you want to reference a value by name - you use it just like any ordinary int, but you cannot change the value. This does not use any RAM. The value is used as the actual value wherever it is referenced, just like #define.

You can use #define where you would use "const int", but it lacks the strong type casting of the "const int". It is best to stick to using #define for optional compilation of blocks of code (combined with #if, #ifdef, etc).

If you add cast qualifiers to the value of a #define, then it is just the same as using a "const int". The compiler will produce code that is, if not identical, at least close enough as to be indistinguishable by the average mortal :wink:

3 Likes

Thanks a lot for the answers, I have a much more clear panorama now.

And now that we see that one of the advantages is to "save RAM" I have one question, that I think it's related with this.

If I use

const int ThisIsTheLedOnTheRightColorRed = 10;

will that take more bytes than something like

const int LR = 10;

When compiling, it doesn't matter how long my variables are, the program will replace it with a "10" or equivalent, I know it's not exactly 10, but it doesn't matter the variable, right?

I ask, bc I noticed something of this, when trying to make my own digitalWrite function, where I called the function dW() so I thought, now the code instead of having 100 calls to "digitalWrite" will have 100 calls to "dW" so I'm writing less bytes, but I realized it doesn't work that way :stuck_out_tongue:

Am I crazy right? I'm trying to re-invent the wheel? ]:smiley:

:sleeping: keep dreamming....

The number of characters in a variable name will have no impact on the size of your program or its use of RAM.

1 Like

It should be mentioned, that #define is a preprocessor operator, doing text replacement. #define PI 3.141 will not define a variable, it just replaces every time where PI is found in the source code with 3.141. And there can be the problem, if you write #define PI 3.141o it is fine until the compiler comes and finds out that there is the letter o where it cannot be! Doing so with a variable will show you immideately that there is something wrong. You can get errors even in different files and should be used with utmost care!

1 Like