0

I'm writing a C header file that defines quite a few constants (mostly ints) that I use in different .m files. I'm doing this to avoid having magic numbers all over my code. Right now I'm using #define for this, but is it better to do it some other way? Some other answers here seem to recommend using const to declare the constants, but when I look at some standard header files (like math.h), they use #define.

Am I even writing a header file for the right reason?

nonex
  • 233
  • 3
  • 12
  • It looks like in that topic most people recommend static const, although I think they are referring to its use in a .c file. Does that guideline still apply when declaring constants in a header file? – nonex Feb 26 '14 at 16:37
  • In a header this could be a little bit more complicated because of [multiple definition errors](http://stackoverflow.com/questions/9049320/linking-issue-with-multiple-definition-of-compilation-error). So it depends heavily on the data type. But for int's it should be ok. – exilit Feb 26 '14 at 16:44
  • Thanks. Am I right in using a header file for this purpose? – nonex Feb 26 '14 at 16:52
  • @nonex If you're going to use these constants in more than 1 file, then yes, you are right in using a header file. – Filipe Gonçalves Feb 26 '14 at 17:29
  • The "static const" vs "#define" in C *may not* be a valid candidate for dup, as *answers* say nothing about headers – manuell Feb 26 '14 at 18:07

0 Answers0