Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Difference between #define and const

  1. May 11, 2008 #1
    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.
     
  2. jcsd
  3. May 11, 2008 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    One of them informs the preprocessor to do a textual substitution. The other actually declares a (constant) C variable.
     
  4. May 11, 2008 #3

    rcgldr

    User Avatar
    Homework Helper

    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).
     
  5. May 13, 2008 #4

    zyh

    User Avatar

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?