Tutorials defining variables instead of using #define.

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've came across a tutorial or two that does something like the following. They define an int variable instead of using a #define macro, e.g. :

 

int CHARGER_OUTPUT	12		// Define pin 12 as charger output

void setup() 
{
  pinMode(CHARGER_OUTPUT, OUTPUT);
}

void loop()
{
  // main loop code
]

 

whereas what I would do is:

 

#define CHARGER_OUTPUT	12		// Digital pin 12 as output for charger on/off

void setup() 
{
  pinMode(CHARGER_OUTPUT, OUTPUT);
}

 

 

 

 

The first method seems very wrong to me as it's both defining a variable which needlessly takes up SRAM. The other thing is, the fact that it's a variable means it could accidentally get its' value changed in the subsequent code.

 

So would you just say the tutorials I've seen are bad advise, or am I missing something.

 

Keith.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By using a variable, you get a degree of type checking. Hopefully the compiler will identify the variable as being constant and treat it as such, so the potential downside of consuming ram has been mitigated. The wonders of modern optimising compilers.

Last Edited: Sat. May 13, 2017 - 02:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Russell,

 

that's very interesting to know. I'm sure one of the examples I seen was by a guy that had a company which wrote compilers. He's very particular with all sorts in programming, like casting everything and never doing "silent casts". So maybe he's defining the "constant" like that with some good reason.

 

Keith.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think it is a technique i should adopt for my C coding as it is something that i do in other languages.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the "variable" really is a constant then you can declare it 'const'. Prevents from unintentionally changing the value (though there's always aliasing...) and might be used by the optimiser to do a better job.

EDIT: Brain slowly warms up.. It should probably be 'static const'. More of his this when I get to the laptop and can type with ease.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. May 13, 2017 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The error here is indeed that the author forgot the "const". In C++ (not C so much) it is the usual technique to define constant literals as "variables" rather than preprocessor macros because C++ is much more stringent about type checking assignment. But this is done on the basis that the variable will be optimised away but that will only happen with "const"
.
EDIT : yup as Johan says "static" too.

Last Edited: Sat. May 13, 2017 - 07:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not a C++ expert but, AIUI, this is one of the areas where C++ is significantly different from C.

 

In 'C', a const variable still consumes memory - but in C++, it may not.

 

Thus, in C++, a const variable can be just like a #define - but with the added bonuses of type & scope.

 

cppreference.com wrote:
Objects declared with const-qualified types may be placed in read-only memory by the compiler, and if the address of a const object is never taken in a program, it may not be stored at all.

 

http://en.cppreference.com/w/c/l...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. May 13, 2017 - 07:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Artandsparks wrote:
I've came across a tutorial or two that does something ...

It would be helpful, when referring to stuff like this, to always give a link - so that people can see it in the actual context.

 

Now that you have the explanation, please contact the author(s) in question ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As awneil says, C differs from C++ re const. In C it consumes memory, although that might be optimized away under certaicircumstances. IIRC there's also something with sizes of arrays, but I need to look that up before saying more.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]