Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. When to use constant variable, and when to use preprocessor/macro constants?

When to use constant variable, and when to use preprocessor/macro constants?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 5495012
    wrote on last edited by
    #1

    I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?

    CPalliniC S S S 4 Replies Last reply
    0
    • U User 5495012

      I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Usually a constant variable is preferred (because it is typed). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • U User 5495012

        I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?

        S Offline
        S Offline
        shubhi
        wrote on last edited by
        #3

        Refer these links. I hope these will be sufficient. http://bytes.com/forum/thread212617.html[^] http://forums.sureshkumar.net/data-structures-c-c-vc/38-diff-between-macro-c-nstant.html[^] :)

        "We can't solve problems by using the same kind of thinking we used when we created them"

        1 Reply Last reply
        0
        • U User 5495012

          I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?

          S Offline
          S Offline
          sashoalm
          wrote on last edited by
          #4

          When using macros you should always remember that they're a direct text substitution, not something the compiler does. Imagine that before your source file is passed to the compiler a normal text manipulation program substitutes the macros with their values and this changed text of your source file is sent to the compiler. One case when macros might be better than constants is when declaring arrays.

          #define MAX_PATH 260
          char arr[MAX_PATH];

          always works, but

          const int MAX_PATH=260;
          char arr[MAX_PATH];

          might give you problems on older compilers.

          There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

          1 Reply Last reply
          0
          • U User 5495012

            I have a question as to when a preprocessor/macro constant should be used, and when a constant variable should be used. Or it would be better if i put it in this way, In a program, when ONLY a macro constant MUST be used and not a constant variable? when ONLY a constant variable MUST be used and not any macro constant?

            S Offline
            S Offline
            SRKSHOME
            wrote on last edited by
            #5

            There is no rule when to use constants or when to use preprocessors.. It is always better to use constants because it is typed.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups