Difference between #define and const

  • Thread starter i.mehrzad
  • Start date
  • Tags
    Difference
In summary, const and #define serve different purposes in C programming. #define is used for textual substitution by the preprocessor, while const declares a constant variable that can be accessed globally. Const variables also follow language scoping rules and can be debugged and passed by reference, making them a more flexible and preferred option compared to #define. However, there are still cases where #define may be necessary, and the choice should be based on business value.
  • #1
i.mehrzad
84
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
  • #2
One of them informs the preprocessor to do a textual substitution. The other actually declares a (constant) C variable.
 
  • #3
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).
 
  • #4
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:

What is the difference between #define and const?

The main difference between #define and const is that #define is a preprocessor directive that replaces all occurrences of a given identifier with a specified value, while const is a keyword that declares a read-only variable with a specific value that cannot be changed.

Can I use #define and const interchangeably?

No, #define and const serve different purposes and cannot be used interchangeably. #define is used for creating global constants, while const is used for declaring variables that cannot be reassigned.

Which one is more efficient - #define or const?

In terms of efficiency, #define is more efficient than const because it is processed by the compiler before the code is executed. On the other hand, const is evaluated at runtime, which can cause a slight decrease in performance.

Can I use #define to define function-like macros?

Yes, #define can be used to define function-like macros, which are essentially small snippets of code that can be used to perform a specific task within a program. However, const cannot be used for this purpose.

What are the benefits of using const over #define?

The main benefits of using const over #define are that const provides type safety and allows for better debugging, as the variables declared with const can be easily identified and modified at compile-time. Additionally, const can be used to declare complex data types, while #define is limited to simple data types.

Similar threads

  • Programming and Computer Science
Replies
8
Views
177
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
781
Back
Top