-4

After coming across symbolic constants via K&R's The C Programming Language I fail to see the usage/importance of them. I'd rather use a 'real' constant, say an integer, than using a #define statement. I can see why you would use say...

#define PI 3.14

as opposed to just typing "3.14" in your program but why would you use a #define statement if I could just use...

static const double PI = 3.14;

Are symbolic constants used today or is it just a feature of the language that was more useful 'back in the day'?

What do you recommend I should use for constants?

haccks
  • 104,019
  • 25
  • 176
  • 264
CS Student
  • 1,613
  • 6
  • 24
  • 40

1 Answers1

1

#define is old, and simply textual replacement processed by preprocessor. For example when you do #define PI 3.14, preprocessor simply replaces all occurance of PI by the number literal 3.14

const is newer, and compiler dependent. It could be evaluated at compile time, or at runtime. It also provides scope.

I personally use #define because it is guarrentee to be evaluated at compile time, and it has other flexible uses.

SwiftMango
  • 15,092
  • 13
  • 71
  • 136