Go Down

Topic: #defines in the middle of code (Read 2 times) previous topic - next topic

drhex

In my current project, I have a few #defines in the middle of a sketch, and it seems to work fine. (arduino-0018).
But I've seen posts on the forum indicating that it might be safer to put all #defines at the top. Why is that, and what might happen otherwise?

cr0sh

I'm not sure it really matters, but I am willing to learn otherwise...btw, you really should use the const keyword for constants instead:

http://www.arduino.cc/en/Reference/Define

Also - putting them at the "top" of the code makes it easier for maintenance and debugging...

:)
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

drhex

Quote
you really should use the const keyword for constants


I do. (promise!)  The defines are for things like:

Code: [Select]
#define num(arr) (sizeof(arr) / sizeof(arr[0]))

and I have them in the middle of the code because they are to be used together with a class defined there.

PaulS

There is a bug, at least in 0017 (and not documented as fixed) in how the pre-processor scans the code for pre-process directives. It apparently stopped looking when the first executable statement was encountered.

Quote
I have them in the middle of the code because they are to be used together with a class defined there.

Why isn't the class defined in a separate file?

drhex

Quote
There is a bug, at least in 0017 (and not documented as fixed) in how the pre-processor scans the code


You mean the pre-pre-processor which adds function prototypes?
So as long as I declare functions before they are used, it is possible to have defines anywhere?

Quote
Why isn't the class defined in a separate file?


Because I don't have any Arduino hardware. I write the code and send it to a friend for testing. It's easier to send one file than several.

Go Up