Difference between #define and const

  • Thread starter Thread starter i.mehrzad
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary

Discussion Overview

The discussion revolves around the differences between the preprocessor directive #define and the const keyword in C/C++. Participants explore the implications of using each for defining constants, including their scope, memory usage, and behavior in the context of the language.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Mehrzad questions the differences between #define and const int, noting that one has a datatype while the other does not.
  • One participant explains that #define performs textual substitution while const declares a constant variable.
  • Another participant elaborates that #define can be an immediate value or a macro, whereas const is a variable that remains unchanged. They mention that pointers can be declared to const but not to #define.
  • This participant also discusses the scope of #define being local and its inability to be linked externally, contrasting it with const variables that can be global but occupy memory space.
  • They further note that a compiler might optimize const as if it were a #define if no pointers are involved, and that on some machines, modifying a constant through a pointer could lead to exceptions.
  • A later reply references a FAQ that highlights advantages of const over #define, such as adherence to scoping rules, visibility in debuggers, and the ability to take addresses.
  • This reply also suggests that while const is generally preferred, there are scenarios where #define may still be necessary, emphasizing a context-dependent evaluation.

Areas of Agreement / Disagreement

Participants present multiple viewpoints on the advantages and disadvantages of #define versus const, indicating that the discussion remains unresolved with competing perspectives on their usage.

Contextual Notes

Some limitations include the potential for confusion regarding the scope and memory implications of each approach, as well as the varying behaviors of compilers in optimizing const variables.

i.mehrzad
Messages
84
Reaction score
0
I wanted to know that is there any difference between #define and "const int"(suppose)

Apart from the fact that one is actually assigned a datatype and the other is not.

Thanking you,
Mehrzad.
 
Technology news on Phys.org
One of them informs the preprocessor to do a textual substitution. The other actually declares a (constant) C variable.
 
In addition to Hurkyl's answer ...

A #define is either an immediate value or a macro. A constant is a variable that doesn't change in value. You can delcare a pointer to a const, but not to a #define, although a define could be a pointer (for example "#define PI1234 ((int *)0x1234)".

In C, #defines are local only, so there's no way to make a #define externally available to the linker. Instead, the #define must be included in the source code for all modules that need to access the define. A const variable can be global and accessed via other linked modules, but requires a memory access and occupies space. In assembly, global equates are possible, and linkers typically generate global equates to indicate the bounds of key points within the code, like the start and end of data and code. Global equates don't require a memory access and don't occupy any space.

Within a module, a C compiler could optimize a const as if it were a #define, if there are no pointers declared to the constant. In CPU terms, the const would become an "immediate" value. Other alternatives is that a const variable could be placed in the code area as opposed to the data area since it doesn't change. On some machines, declaring a ponter to a constant could cause an exception if you tried to modify the constant via the pointer (if the constant were placed in a read-only code section).
 
look at here:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7

[29.7] Why would I use a const variable / const identifier as opposed to #define?
const identifiers are often better than #define because:

they obey the language's scoping rules
you can see them in the debugger
you can take their address if you need to
you can pass them by const-reference if you need to
they don't create new "keywords" in your program.
In short, const identifiers act like they're part of the language because they are part of the language. The preprocessor can be thought of as a language layered on top of C++. You can imagine that the preprocessor runs as a separate pass through your code, which would mean your original source code would be seen only by the preprocessor, not by the C++ compiler itself. In other words, you can imagine the preprocessor sees your original source code and replaces all #define symbols with their values, then the C++ compiler proper sees the modified source code after the original symbols got replaced by the preprocessor.

There are cases where #define is needed, but you should generally avoid it when you have the choice. You should evaluate whether to use const vs. #define based on business value: time, money, risk. In other words, one size does not fit all. Most of the time you'll use const rather than #define for constants, but sometimes you'll use #define. But please remember to wash your hands afterwards.
 
Last edited by a moderator:

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
3
Views
3K
Replies
89
Views
7K
  • · Replies 36 ·
2
Replies
36
Views
3K