0

I am writing a c program consisting of 5 modules, each with an associated header file where functions prototypes are written. Only, I have a c file/header pair that I only use for defining constant arrays, and following the logic I used for the other files I declared my constants in the header and initialized them in the c file.

Only it does't feel.. natural. Shoudln't I be declaring those constants using the #define preprocessor directive? Is it a good use case for using #define or am I not correctly understanding its meaning correctly?

(I am already using the #ifndef #define structure in my header file, and I understand that use case)

Balocre
  • 175
  • 2
  • 10
  • If you are not already using preprocessor and don't miss anything, don't start. – Yunnosch Dec 14 '18 at 13:54
  • "What is the use of preprocessor defines?" is quite broad by the way. – Yunnosch Dec 14 '18 at 13:55
  • 3
    [This question](https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum?rq=1) discusses using `#define` for constants vs. other alternatives –  Dec 14 '18 at 13:57
  • Use `define` for your arrays if you *may* want to change them later on. Also, it helps if you have a verbal description of these constants instead of an abstract number. – Jongware Dec 14 '18 at 13:57
  • @usr2564301 What is the benefit of using `#define` for configurable arrays? The syntax is not getting anymore readable. Configurable arrays I tend to put into a file dedicated to configuring arrays. – Yunnosch Dec 14 '18 at 14:00
  • @Yunnosch: I meant during compile time. I make it a habit to declare file read buffers as `data[MAX_READ_SIZE]`, for example. That way I can (a) test what happens when it's too small (as it should not fail on that), and (b) increase the size to optimize memory use against speed. – Jongware Dec 14 '18 at 14:05
  • We could talk about examples if you provided some, but without any, the question is quite broad. In general, however, preprocessor macros serve a somewhat different purpose than do declarations and code. Nothing expressed so far in the question leads me to think that using macros would be practically or stylistically preferable to what you're already doing. – John Bollinger Dec 14 '18 at 14:19

0 Answers0