Post subject: When to use CONST and when to use # define
Hi,
I have a very simple question. In C, what's the difference between #define and CONST ? To me both seem to be same. Is there anything related with the memory allocation ? _________________ Keep your own dignity, both online and offline throughout your life !!!
Sun May 23, 04 6:13 am
Belal User
Joined: 08 Mar 2004 Posts: 84
Location: Dhaka, Bangladesh
Post subject:
with #define we can define a constant and functional macros like
#define DOUBLE(x) (x+x)
that can b use as:
a = DOUBLE(b) * c;
so, i think this is one diff. and i dont' think they have diff like memory addressing or optimization, yes it may vary with the compilier. many likes to use const to define constant and make it clear to them.
if i get anything new at this topic, i will let u know _________________ we've lot of things to think abt curr probs
so, i don't have time to think abt religion or wonder of sceice .......... how ppl can waste their time like this?
Mon May 24, 04 5:40 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject:
Hi,
Technique comes from the C programming language. Constants may be defined using the preprocessor directive, #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345
Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". The problem with this technique is that the replacement is done lexically, without any type checking, without any bound checking and without any scope checking. Every "pi" is just replaced by its value. The technique is outdated, exists to support legacy code and should be avoided.
Const
The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;
There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter lesson. I had a chicken and egg type of problem and my solution was that the chicken is constant and the scope is an egg.
-DK. _________________ ...we too are stardust...
Mon May 24, 04 12:34 pm
Tousif Guest
Post subject:
Hi Guys,
I agree with DK in that explanation. However, one thing I would like to add is the issue of efficiency. When you define a variable to be "const" knowing that the value of it won't change across a certain scope, compiler puts that value in the register during execution of that scope and thus for future references, it desn't have to fetch it from the memory, which it would do otherwise in case of a page fault. So, it improves the performace significantly.
I am not so sure in agreeing with DK about #def being outdated. I hope you didn't mean it for #ifdef's as well. My understanding is that "const" doesn't replace the value the way a preprocessor directive does, so it would still fetch it from the register. Now if you are running a simulation that takes huge memory and runs long loops for days...it makes sense to use #defs.
But then again, since these days most of the cmopilers are pretty smart and does a lot of optimizations by themselves, so it becomes a personal choice I guess.
#defs usually make your code more readable, for example if you are using 3.1415 in a lot of places, instead use PI.
Tousif
Tue May 25, 04 3:43 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject:
Mr. Tousif,
Thank you for pointing out those I missed. I think there is no need for later lesson. I mean outdated casue now a days C++ programmers avoid that style. But again as you said, at the end it comes to personal choice.