tms8c8

If I declare my pins using the normal method, I don't have any errors. When I attempt to utilize "define", I get errors where I try to set the pin mode. I understand that the compiler replaces all instances of the #define "variables" with the numbers and thus saves me some memory. However, I don't understand all the subtleties here.

tms8c8

I'm sorry; I'm not trying to be dense, but why is having a type important?

I see why using #define is inconvenient for the programmer, but are there any "performance" reasons not to use it? In my application, I am storing data in the program memory because EEPROM isn't big enough for all the data and I want it to be there after a power cycle. However, the data is large enough that memory is an issue. That is why I started looking into using the #define method. Being largely ignorant of these matters, are there any performance problems using it?

In my application, I am storing data in the program memory because EEPROM isn't big enough for all the data and I want it to be there after a power cycle. However, the data is large enough that memory is an issue. That is why I started looking into using the #define method.

Sounds like you'd better go back to the beginning and describe your memory space problems, because #define won't be of any help one way or the other in that respect. It is really just a general method of expressing a literal string or numeric constants in a symbolic way, to aid readability and maintainability of a program.

It really is a text processor that rewrites your source file before the compiler proper sees it. So you write

#define PI 3.14159

and write

double theta1 = PI/2.0;double theta2 = = PI/4.0;

what the compiler will see is

double theta1 = 3.14159/2.0;double theta2 = 3.14159/4.0;

To the compiler, it's as if PI never existed.

You can see why it is useful to avoid the sort of errors you might get by writing out the literals explicitly each time. You will probably end up getting things like

double theta1 = 3.14159/2.0;double theta2 = 3.14259/4.0;

and have a lot of fun trying to figure out why your circles aren't exactly round...

tms8c8

I don't currently have a memory problem; I'm just very conscious of the amount of memory I'm using. I saw some passing references to #define and thought it might be advantageous to use it. If I had paused to think, I would have remembered that using "const" accomplishes the same thing (in regard to memory) although this brings me to my next question!

When and why would I use #define instead of const "type" to declare variables? Is the #define a relic and using const "type" always the better choice?

I would say always use const instead of #define when you have... er... to declare a named constant Remember that #define is just like test substitution, while with const you have the usual compile-time type checking.

Using something like the snippet above allows you to optionally have a StartButton. However, because the compiler does an excellent job of dead code elimination, the same thing can be accomplished using typed constants...

When and why would I use #define instead of const "type" to declare variables? Is the #define a relic and using const "type" always the better choice?

It really depends on the situation. A const variable is really a variable, at least so far as the compiler is concerned, and so all the code needs to be syntactically correct to compile. With the macro preprocessor, since it is just manipulating text (your source code) in various ways, it can be more flexible, particularly in cross platform programming, where the required syntax on one system won't even compile on the other.

and so on. The other thing to realise is that a const variable will obey the usual scoping rules for variables, this isn't the case for #define macros (which may be a help or a hinderence depending on the context).

It's really horses for courses. Have lots of tools of tools in the toolbox, and keep them all sharp.

Most of my code usually ends up being compiled on several platforms for one reason or another, and trying to maintain that without using the macro preprocessor would be very tiresome and error-prone.