Search code examples
c++constantsc-preprocessor

Why would someone use #define to define constants?


It's a simple question, but why would someone use #define to define constants?

What's the difference between

#define sum 1 and const int sum = 1;


Solution

  • #define has many different applications, but your question seems to be about one specific application: defining named constants.

    In C++ there's rarely a reason to use #define to define named constants.

    #define is normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short, const int objects are not constants in C, which means that in C the primary way to define a true constant is to use #define. (Also, for int constants one can use enums).