Using a previously defined #define in a new #define in C
c, c-preprocessor
Solution
A `#define` is handled by the pre-processor. The pre-processor is run prior to compilation and can perform simple mathematical operations and copy/paste of code. For instance, you could do the following with your example:
`int myVar = SAMPLERATE;`
The pre-processor would simply paste `32` where `SAMPLERATE` is before being compiled.
This mechanism is powerful in the sense that you have now created a name for an integer value. This adds meaning for both you and future developers. It also allows you to make changes in one place instead of many.
Just be sure to `#define SAMPLERATE 32` prior to any other `#define` statements that may use `SAMPLERATE`.
Problem
Are there any potential issues/dangers in doing something such as ``` #define SAMPLERATE 32 // Sample rate in hertz #define ONE_MINUTE ( SAMPLERATE * 60 ) #define FIVE_MINUTES ( ONE_MINUTE * 5 ) ``` My compiler doesn't issue any errors or warnings. It's very nice because I can change one `#define` value (SAMPLERATE) and all the others get set to what they need to be with no other changes. I'm just not entirely sure if this is best practice or safe.