When to use constant variable, and when to use preprocessor/macro constants?
-
I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?
-
I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?
Usually a constant variable is preferred (because it is typed). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?
Refer these links. I hope these will be sufficient. http://bytes.com/forum/thread212617.html[^] http://forums.sureshkumar.net/data-structures-c-c-vc/38-diff-between-macro-c-nstant.html[^] :)
"We can't solve problems by using the same kind of thinking we used when we created them"
-
I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?
When using macros you should always remember that they're a direct text substitution, not something the compiler does. Imagine that before your source file is passed to the compiler a normal text manipulation program substitutes the macros with their values and this changed text of your source file is sent to the compiler. One case when macros might be better than constants is when declaring arrays.
#define MAX_PATH 260
char arr[MAX_PATH];always works, but
const int MAX_PATH=260;
char arr[MAX_PATH];might give you problems on older compilers.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
-
I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?