#define or const?
Hi
I was just wondering is there any real advantage in using #define for constants instead of const? Surely in the case of const, the compiler replaces all references with the actual value, much like the preprocessor does for #define, so where is the advantage? Personally, I use const for type-safety and consistency. Should I switch to #define?
I think the compiler is faster at it or something.
Also you can set the variable type, you know int, char and stuff.
Also you can set the variable type, you know int, char and stuff.
==============================From The Magical World Of Tempermental Rose
...
Why would you ever want to switch to #define?
There are no advantages, just disadvantages.
Why would you ever want to switch to #define?
There are no advantages, just disadvantages.
In straight C, #define was the ONLY method. C++ added the keyword const. const is prefered in C++ and should probably be use because it does allow type checking to occur. However, on is not inherently better than the other. Use what is comfortable for you. The big thing is to stay consistent.
One thing to remeber, though, is that other preproccessor directives CANNOT check const values. If you are using the preproccessor to conditionally compile code - it''s gonna have to be #define.
Hope this helps,
Landsknecht
One thing to remeber, though, is that other preproccessor directives CANNOT check const values. If you are using the preproccessor to conditionally compile code - it''s gonna have to be #define.
Hope this helps,
Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
But folks whinned and I had to change it.
If you''re using #defines to define constant values, you should just move to const. Their value shows up when using a debugger and I find that quite helpful.
If you''re using #defines to define macros, stop. Use inline functions. They''re a lot safer and in the long run, you''ll save a lot of time that would have been spent debugging.
As far as C goes, const is a keyword in C. It is part of the C99 standard. I''m not sure about the C90 standard but K&R''s book on ANSI C does cover the use of const, so I''m assuming it''s part of that standard as well. I could be wrong on this, so take it with a pinch of salt.
If you''re using #defines to define macros, stop. Use inline functions. They''re a lot safer and in the long run, you''ll save a lot of time that would have been spent debugging.
As far as C goes, const is a keyword in C. It is part of the C99 standard. I''m not sure about the C90 standard but K&R''s book on ANSI C does cover the use of const, so I''m assuming it''s part of that standard as well. I could be wrong on this, so take it with a pinch of salt.
![](smile.gif)
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Picking up on this:
for defining values const is indeed better because of the additional type checking.
#define in the context of macros can sometimes be unavoidable, specially for highly complex macros with text replacement. The dodgy template implementation in VC++ make it impossible to do some things with them that CAN be done using macros. But they sure aren''t pretty to look at.
As an aside, the "C standards" argument is kindof a moot point. It''s been said before that lots of good things from C++ are included in the C99 standard, but if no-one implements them in mainstream compilers, there''s not all that much use for them.
The same thing goes for C++, see for instance my mention above of VC++''s not-exactly-full-featured implementation of the C++ standard for templates.
New standards take quite a while to filter down into the most-often-used compilers. Sometimes I wish they''d get on with it! (eg, partial specialisation for templates)
People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
for defining values const is indeed better because of the additional type checking.
#define in the context of macros can sometimes be unavoidable, specially for highly complex macros with text replacement. The dodgy template implementation in VC++ make it impossible to do some things with them that CAN be done using macros. But they sure aren''t pretty to look at.
As an aside, the "C standards" argument is kindof a moot point. It''s been said before that lots of good things from C++ are included in the C99 standard, but if no-one implements them in mainstream compilers, there''s not all that much use for them.
The same thing goes for C++, see for instance my mention above of VC++''s not-exactly-full-featured implementation of the C++ standard for templates.
New standards take quite a while to filter down into the most-often-used compilers. Sometimes I wish they''d get on with it! (eg, partial specialisation for templates)
People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Thanks for your replies.![](smile.gif)
I thought using const was probably better, but I have seen #define being used so much for constants that I thought people must be doing it for some reason.
![](smile.gif)
I thought using const was probably better, but I have seen #define being used so much for constants that I thought people must be doing it for some reason.
Hypocritical point: Use enums. They are the way to go.
I still use #defines, because of the way I was taught. Hopefully I will switch someday.
I still use #defines, because of the way I was taught. Hopefully I will switch someday.
VK
One reason to probably use preprocessors is for complete portability for your program.
#ifdef __BORLANDC__
#include
#elif define (_MSC_VER)
#include
#endif
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
#ifdef __BORLANDC__
#include
#elif define (_MSC_VER)
#include
#endif
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
![](http://www.crosswinds.net/~druidgames/resist.jpg)
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement