top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the benefit of using #define to declare a constant ? [CLOSED]

+4 votes
287 views
What is the benefit of using #define to declare a constant ? [CLOSED]
posted Dec 24, 2013 by Prachi Agarwal

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
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 cast it, 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.

...