Difference between #define and global constant

  • JETeran
    Likes 0

    Problem Description

    Greetings.

    We are creating a file called Definitions.h where we wrote:

    #define DISPLAY_TIME_SPLASH_SCENE 2
    #define TRANSITION_TIME 0.5

    Then we use both defines in the code. But my question is, why should we use #define instead of declaring a global constant with the same valor? 

    Thanks once again!

  • Sonar Systems admin
    Likes 0

    When you say a global constant do you mean a global constant variable?

  • JETeran
    Likes 0

    Hello there.

    Yes indeed. For what I know about programming (still learning!) is that we can declare a global constant variable to use it in many parts of the code. What is the difference between this and declaring it as a #define ?

  • Sonar Systems admin
    Likes 0

    Take a look at this conversation, this should help you http://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum

     

  • JETeran
    Likes 0

    It did, thank you.

    Reading further, I found out that using #define is the less recommended by developers, do you also agree? As everything, has is Pros and Cons. Anyways, I will continue working as you are teaching me!

  • Sonar Systems admin
    Likes 0

    I personally prefer #defines, but honestly find what works for you and the situation you are programming for.

     

     

    Loads of stuff are considered bad coding practise but many big companies will practise and even encourage them because it makes life easier.

  • JETeran
    Likes 0

    Thanks so much for sharing your preferences.

    I will continue using them as you do in the tutorials. Thanks once again!


  • Sonar Systems admin
    Likes 0

    Your very welcome, do no hesitate to ask anymore questions.

Login to reply