66

Possible Duplicates:
Why would someone use #define to define constants?
difference between a macro and a const in c++
C++ - enum vs. const vs. #define

What is the difference between using #define and const for creating a constant? Does any have a performance advantage over the other? Naturally I prefer using the const but I'm going to consider the #define if it has suitable advantages.

Community
  • 1
  • 1
afaolek
  • 8,452
  • 13
  • 45
  • 60

6 Answers6

115

The #define directive is a preprocessor directive; the preprocessor replaces those macros by their body before the compiler even sees it. Think of it as an automatic search and replace of your source code.

A const variable declaration declares an actual variable in the language, which you can use... well, like a real variable: take its address, pass it around, use it, cast/convert it, etc.

Oh, performance: Perhaps you're thinking that avoiding the declaration of a variable saves time and space, but with any sensible compiler optimisation levels there will be no difference, as constant values are already substituted and folded at compile time. But you gain the huge advantage of type checking and making your code known to the debugger, so there's really no reason not to use const variables.

Pharap
  • 3,826
  • 5
  • 37
  • 51
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 37
    An important aspect of this is that preprocessor macros don't have any scope, while `const` values do. – Fred Larson Jun 22 '11 at 15:25
  • 2
    @Fred: That, and much more -- there is no type checking or even syntax checking. It's literally just textual replacement. In the #define-vs-const dichotomy, macros just paste literals into the code... – Kerrek SB Jun 22 '11 at 15:28
  • Since using **const** requires a lookup overhead for the compiler (am I right?), #DEFINE would be better for values like the mathematical pi (since the value is just replaced before compilation). – afaolek Jun 22 '11 at 15:42
  • 2
    @afaolek: Test it and compare the assembly. Any decent compiler will substitute constants right in if possible, so there should be no difference. (And with the macro, the compiler will have to parse and lex and catalogue the literal each time.) – Kerrek SB Jun 22 '11 at 15:49
  • 3
    It seems that using `const` is preferable to `#define` - then why almost all the code bases in C use `#define` extensively in header files for example, instead of `const`?? – Corel Mar 05 '23 at 08:48
12

#define creates an entity for substitution by the macro pre-processor, which is quite different from a constant because depending on what you define it will or will not be treated as a constant. The contents of a #define can be arbitrarily complex, the classic example is like this:

#define SQR(x) (x)*(x)

Then later if used:

SQR(2+3*4)

That would be turned into:

(2+3*4)*(2+3*4)
zippy
  • 1,228
  • 10
  • 19
4

The difference is that #define is processed by the preprocessor doing what amounts to simple text replacement. Const values defined like this are not visible for the actual compiler, while a variable defined with the const modifier is an actual typed "variable" (well not really that variable). The disadvantage of #define is that is replaces every occurence of the name, while const variables get normal lookup, so you have less risk of naming conflicts and it's not typesafe.

The advantage of #define is that it guarantees constness and therefore there will be no backing variable. Const Variables may or may not be substituted into the code, so #define might be faster in some situations. However a good compiler should inline those consts anyways and it's unlikely to make much of a difference in most situations, so I would keep using const unless you have a piece of code where you have seen that the compiler hasn't inlined the variable and it is very, very performance critical code.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
1

#define is textual replacement, so it is as fast as it can get. Plus it guarantees constness. The downside is that it's not type-safe.

On the other hand, const variables may or may not be replaced inline in the code. You can cast away the constness, forcing it to be in memory (although it probably resides in read-only memory to begin with, but there's headaches either way). It is guaranteed to be type-safe though since it carries its own type with it.

I would personally recommend const to make your intent clear.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • You can't legally cast away the constness of a const variable, and it's undefined whether doing so illegally will force it into memory. – Mike Seymour Jun 22 '11 at 15:38
  • Sure you can, whether the result is UB or not is another story. And that is my point, you can't prevent it using `const`. – Blindy Jun 22 '11 at 16:09
  • @Blindy: OK, but your answer says that it forces it into memory, when it doesn't necessarily. It could just as well carry on inlining it where it's used correctly, and ignore any attempt to change it; in fact, that is exactly what my compiler does (at least some of the time). – Mike Seymour Jun 22 '11 at 16:18
  • What you can do legally is take the address of a const variable, forcing it into memory. That is something you could prevent with `#define`. – Mike Seymour Jun 22 '11 at 16:20
  • That's another way to break `const`, sure, but even casting `const` away should get rid of any inlining you can do. I can't remember where I read it though. – Blindy Jun 22 '11 at 16:29
  • @Blindy: no, attempting to modify it by casting `const` away gives undefined behaviour, so you cannot say that it "should" do anything. You may have read that one particular implementation places it in memory and allow you to modify it; other implementations do not. – Mike Seymour Jun 22 '11 at 16:38
1

DEFINE is Preprocessor instruction, For example #define x 5. Compiler takes this value and insert is where ever you are calling x in the program and generate the object file. Define constants deosn't create a symbol entry in symbol table. IF you want to debug the program , you will not find x . Use constant where ever possible that what i think.

Alok
  • 1,997
  • 2
  • 18
  • 30
0

#define A B tells the preprocessor (a part of the compiler) to substitude B wherever it sees A in the code, and it does it before compiling the code. You could (although it's a terrible idea) do something like #define FALSE TRUE.

A const variable means that once the variable is set it can't be changed, however it doesn't do anything with the preprocessor, and is subject to the normal rules of variables.

RC Howe
  • 509
  • 3
  • 16