0

suppose i have defined constant using #define MAX 5 and i am using it in function called find() as shown below:

find()
{
if (MAX==5)
   {
   //do this;
   }
else
   {
   //do this;
   }

}

** If i am calling this function 100 of times** then which way is better to define constant as we can define it with 2 ways
1.#define MAX 5
2.const int MAX=5

3
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by deceptikon
0

If you have multiple MAX macros, the compiler has to reserve memory for each occurence.
ie. MAX + MAX - MAX is 3 substitutions of integer 5, so 4*3=12 bytes reserved
If you declare a constant, it's only declared and initialized once in memory, so it takes up only 4 bytes.

And when you're working with a debugger, it can tell you the value of the constant but not for macros.

0

Thanks for your reply.
But here i have defined MAX one time only and it will get substitued by 5 before compilation begins.So which one is better?

0

But here i have defined MAX one time only and it will get substitued by 5 before compilation begins.So which one is better?

Neither. If you're comparing a constant with a constant then you should just strip away the test entirely because it's not dependent on runtime values, or replace it with conditional inclusion:

#if MAX == 5
    /* Do this */
#else
    /* Do this */
#endif

Otherwise you're just saying "always do one part of the if statement, but I still want the overhead of the test". It's silly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.