Question:

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

by  |  earlier

0 LIKES UnLike

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

 Tags:

   Report

2 ANSWERS


  1. You code might run faster, depending on the compiler.

    #DEFINE makes a CONSTANT.  As opposed to a variable, like "B=5".

    Suppose you write code that says

    #define COUNT 5

    Let B1 = 3

    Let C1 = B1

    Let D1 = COUNT

    your compiler will generate

    **Let B1 = 3

    lda  3

    sta  B1

    ** Let C1 = B1

    lda   B1

    sta  C1

    ** Let D1 = COUNT

    lda  5

    sta  D1

    In this little progrm there are two pre-defined values, B1 and COUNT.  B1 is a variable and COUNT is a CONSTANT.

    Look how much code it take to load C1 with the variable B1, compared to how much code it take to load D1 with the constant COUNT.  In this case using a CONSTANT is almost twice as fast!

    The limitation is that the progrm canNOT change a CONSTANT -- to do that you have to recompile.  Programs can always change variables, but the resulting code may be slower.


  2. if you mean in C programming, it's because then you can't accidentally modify the value, in comparison to storing it in a variable in math expressions

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions