Why does someone use #define to define constants?

This is a simple question, but why would anyone use #define to define constants?

What is the difference between

#define sum 1 and const int sum = 1;

+42
c ++ c-preprocessor const
Jun 08 '11 at 3:50
source share
8 answers

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

In C ++, there is rarely a reason to use #define to define named constants.

#define usually widely used in C code, since the 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 main way to determine the true constant is to use #define . (You can also use enums for int constants).

+34
Jun 08 2018-11-11T00:
source share

No one should! In fact, you should prefer const int sum = 1; above #define sum 1 for a number of reasons:

Visibility Based Mechanism:

#define not realm, so there is no way to create a namespace with a limited class. Although constant variables can be limited in classes.




Avoid weird magic numbers with compilation errors:

If you use #define , they are replaced by the pre-processor during pre-compilation. So if you get an error message at compile time, it will be confused because the error message will not refer to the macro name, but the value and it will appear a sudden value, and everyone could spend a lot of time tracking this code .




Easy debugging:

Also for the same reasons stated in # 2, while debugging #define will not give any help.




Thus, to avoid the above situations, const would be a better choice.

+30
Jun 08 2018-11-11T00:
source share

In the example you just gave, I would usually use a constant. Except, of course, #define can be used to conditionally compile elsewhere:

 #if SOME_DEFINE == 1 // Conditional code #endif 

This is what you cannot do with a constant. If you do not need a value accessible from the preprocessor, I would say use const if there is no reason why this is not possible. There are some things on this in the C ++ FAQ lite , where they rightly point out that just because the preprocessor is "evil" does not mean you will never need it.

+8
Jun 08 2018-11-11T00:
source share

#define needs to do things like enabling guards, because C ++ does not have a real module import system.

#define causes a literal text replacement. The preprocessor understands how tokenize the source code, but has no idea what this really means. When you write #define sum 1 , the preprocessor looks at your code and looks for each instance of the sum token and replaces it with token 1 .

This has a number of limitations: #define sq(x) x * x will not work correctly if you use it as sq(3+3) ; and using #define for a constant is by no means a scope and does not bind any type to a constant. However, #define can be used (especially in combination with some other special elements, such as preprocessor operators # and ## ), to make magic that is otherwise impossible (except that it manually does what the preprocessor does )

+3
Jun 08 2018-11-11T00:
source share

Always try to use "const int" rather than #define.

Use #define only if your preprocessor code can be read by another tool and it’s easier to go with the preprocessor rather than parse the language.

Also, this is the only way to determine something to be checked later, # if / # else / # endif

+2
Jun 08 2018-11-11T00:
source share

In simple words

 #define sum 1 is pre processed 

This means that sum does not exist after the preprocessing step is completed.

 const int sum = 1; is compiled/linked 

This means that sum exists until the executable file is created from your program.

0
Jun 08 2018-11-11T00:
source share

const int is just an int that cannot change. #define is a directive for the C preprocessor, which is much more than just defining constants.

See here http://en.wikipedia.org/wiki/C_preprocessor for more details.

0
Jun 08 2018-11-11T00:
source share

Firstly, this is a preprocessor directive, before the compiler compiles your code, it will go over and replace the sum with 1. The second declares a variable in memory to store this amount. I'm sure it can be argued that it is better, but "const int" is probably more common in C ++ (when it comes to numerical constants).

http://www.geekpedia.com/KB114_What-is-the-difference-between-sharpdefine-and-const.html

-one
Jun 08 2018-11-11T00:
source share



All Articles