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. Multiple #define with same name

Multiple #define with same name

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 8 Posters 0 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.
  • D Offline
    D Offline
    dipuks
    wrote on last edited by
    #1

    What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

    L A L M T 6 Replies Last reply
    0
    • D dipuks

      What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Who knows? That's why the compiler is giving you a warning. It'll probably be defined differently in different modules.

      L u n a t i c F r i n g e

      1 Reply Last reply
      0
      • D dipuks

        What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

        A Offline
        A Offline
        Abhi Lahare
        wrote on last edited by
        #3

        First I guess, this is bad design, why ? two definition, you should have ideally one define ( in header file) and include that file where the value being used. On other hand technically if two files are unrelated files, each should have there respective values. If one file is calling function in another or they are two separate header files, included in a single cpp file, you compiler should give error,

        1 Reply Last reply
        0
        • D dipuks

          What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

          L Offline
          L Offline
          loyal ginger
          wrote on last edited by
          #4

          The compiler gives a warning in this case because even though this practice is totally acceptable, it may indicate some type of oversight. In cases like this, the compiler's warning can be ignored if it is intentional; otherwise, it should be corrected. As for why this is acceptable by the compiler, it is because it does not violate any rules of the language. The value of the defined symbol when it is referenced depends on the order of the two #defines that was read by the compiler when it is in the preprocessing stage. The preprocessor will use the value of the one that appeared cloest to the reference to substitute the symbol. By compile time this substitution is already done. I would avoid this practice since it is error prone.

          1 Reply Last reply
          0
          • D dipuks

            What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            If the compiler gives a warning, fix it. either rename one of the offending define or depending on what you want to do, you can try something like this to re assign value to a #defined value:

            #define MY_NAME 1

            #ifdef MY_NAME
            #undef MY_NAME
            #define MY_NAME 3
            #endif

            Watched code never compiles.

            L 1 Reply Last reply
            0
            • M Maximilien

              If the compiler gives a warning, fix it. either rename one of the offending define or depending on what you want to do, you can try something like this to re assign value to a #defined value:

              #define MY_NAME 1

              #ifdef MY_NAME
              #undef MY_NAME
              #define MY_NAME 3
              #endif

              Watched code never compiles.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, there is no need to test (see MSDN[^]), you can reduce it to:

              #undef MY_NAME
              #define MY_NAME 3

              as #undef never complains about undefined symvols. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              M 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, there is no need to test (see MSDN[^]), you can reduce it to:

                #undef MY_NAME
                #define MY_NAME 3

                as #undef never complains about undefined symvols. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                Yep you're right. :)

                Watched code never compiles.

                1 Reply Last reply
                0
                • D dipuks

                  What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

                  T Offline
                  T Offline
                  T2102
                  wrote on last edited by
                  #8

                  With #define constants, you should try to surround the value with paranthesis. It also is safer to check if there is a name conflict and throw an error if there is. You do not want to use the wrong constant in another file! This could happen with the use of #undef #ifdef MY_NAME #if MY_NAME!=(3) #error Conflicting definitions of MY_NAME #endif #else #define MY_NAME (3) #endif

                  1 Reply Last reply
                  0
                  • D dipuks

                    What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    dipuks wrote:

                    But what will be the value of "MY_NAME" when it is used? is it 1 or 3?

                    You would have spent less time just trying it yourself. :rolleyes:

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    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