top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the benefit of using #define to declare a constant?

+5 votes
524 views
What is the benefit of using #define to declare a constant?
posted Dec 6, 2013 by Neeraj Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+3 votes
 
Best answer

Using the #define method of declaring a constant enables you to declare a constant in one place and use it throughout your program. This helps make your programs more maintainable, because you need to maintain only the #define statement and not several instances of individual constants throughout your program.

For instance, if your program used the value of pi (approximately 3.14159) several times, you might want to declare a constant for pi as follows: #define PI 3.14159

Using the #define method of declaring a constant is probably the most familiar way of declaring constants to traditional C programmers. Besides being the most common method of declaring constants, it also takes up the least memory.

answer Dec 6, 2013 by Prachi Agarwal
+2 votes

By defining constant using #define gives you the following advantages :

  1. DEBUGGING : Helps you during debugging as you have to check only at one place where you did #define .
  2. ACCURACY : Let us take value of pi = 3.14 , but you are not getting accurate output , then at only one place you have change i.e "#define pi 3.142" and you will get accurate output.

    1. TIME : It save your time. As in software company time is indirectly proportional to money.

By defining constant without using #define gives you following disadvantages :

1.DEBUGGING: Debugging will take more time , as you have to see lines of code where you have used the constant.At the end if you miss one also then headache will start. So to save head from headache use macro with constants.
2. ACCURACY : For getting accurate result again you have to visit every lines of code and tension starts . So by using macro you will be tension free.
3. TIME: You will take more time. And from client point of view client will never pay more money to those company who takes more time. They want every thing within time and this small small concept will help you to complete your assigned work within time. And at the end you will be rewarded and company will finish his project within time and make good money with good reputation with client.

I hope I have answered correctly , please let me know if I am wrong.

answer Dec 29, 2013 by Pankaj Kumar
0 votes

When the value of the constant changes, all the places where it has been referred need to be changed. Instead, when we use # define, changing it in # define and compiling it again, should handle the replacement.

And since # define is a pre-processor directive, it has added advantage i.e. fast.

answer Dec 7, 2013 by Salil Agrawal
Similar Questions
0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

0 votes

Lot of confusion and lot of scattered info, can someone help me by providing to the point response.

...