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. What is #if 0

What is #if 0

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 Posts 11 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.
  • P Prakash Nadar

    Imtiaz Murtaza wrote:

    I've seen in many source codes, some statements written in #if 0 block something like: #if 0 printf("Blah Blah"); #endif Please tell me what is the meaning of #if 0 in the code above

    it simply means the code in side the block is ignore for compilation and will not appear in the final build. it is similar to saying #if !defined(SOME_BUILD_FLAG) or #if FALSE #endif since the dev wanted the code to be explicitly removed from compilation he used #if 0 #endif directive


    -Prakash

    I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #3

    Mr.Prakash wrote:

    it simply means the code in side the block is ignore for compilation and will not appear in the final build.

    Then what is the purpose of the code inside that block ? For example, if the code is written in #ifdef DEBUG block, it is skipped in the release builds but its purpose is clear and that is: it will be compiled in debug builds. So what is the purpose of writing code in #if 0 when i'll never be present in the executable.

    K 1 Reply Last reply
    0
    • I Imtiaz Murtaza

      Mr.Prakash wrote:

      it simply means the code in side the block is ignore for compilation and will not appear in the final build.

      Then what is the purpose of the code inside that block ? For example, if the code is written in #ifdef DEBUG block, it is skipped in the release builds but its purpose is clear and that is: it will be compiled in debug builds. So what is the purpose of writing code in #if 0 when i'll never be present in the executable.

      K Offline
      K Offline
      kevincwong
      wrote on last edited by
      #4

      There are many reasons to use #if 0 Actually, I do it all the time. Here is my reason. Let say I re-write some code/method/function and I think it should work. But I would like to keep the old code around. Just in case...... I know I can retrieve the old code from the source control. However, it's more convinence if I have the old code right next to the new code. Kevin

      S 1 Reply Last reply
      0
      • K kevincwong

        There are many reasons to use #if 0 Actually, I do it all the time. Here is my reason. Let say I re-write some code/method/function and I think it should work. But I would like to keep the old code around. Just in case...... I know I can retrieve the old code from the source control. However, it's more convinence if I have the old code right next to the new code. Kevin

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #5

        I hate #if 0:mad:. It's not so bad if the code commented out is small, but when large pieces of code are bracketed with these you can find yourself analyzing code only to find, when you get to the top, that you've been wasting your time and the code isn't even part of the product. This has happened to me many times. If the programmer simply commented it out the syntax highlighting will stop such problems from ever occurring. Steve

        T K D P 4 Replies Last reply
        0
        • S Stephen Hewitt

          I hate #if 0:mad:. It's not so bad if the code commented out is small, but when large pieces of code are bracketed with these you can find yourself analyzing code only to find, when you get to the top, that you've been wasting your time and the code isn't even part of the product. This has happened to me many times. If the programmer simply commented it out the syntax highlighting will stop such problems from ever occurring. Steve

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #6

          Stephen Hewitt wrote:

          I hate #if 0

          me too. i use this purpose instead...

          #ifdef DEBUG
          #define DPRINT printf
          #else
          void DPRINT(...) {}
          #endif

          this way, in debug mode, we could DPRINT() something, which will not been took in account in release mode (and more, the compiler will optimize a call to DPRINT() in release). for instance :

          int Foo() {
          DPRINT("Foo()\n");
          int i = 654;

          // modification of i...
          
          DPRINT("Foo output : %d\\n", i);
          return i;
          

          }


          TOXCCT >>> GEII power
          [toxcct][VisualCalc 2.20][VisualCalc 3.0]

          P 1 Reply Last reply
          0
          • T toxcct

            Stephen Hewitt wrote:

            I hate #if 0

            me too. i use this purpose instead...

            #ifdef DEBUG
            #define DPRINT printf
            #else
            void DPRINT(...) {}
            #endif

            this way, in debug mode, we could DPRINT() something, which will not been took in account in release mode (and more, the compiler will optimize a call to DPRINT() in release). for instance :

            int Foo() {
            DPRINT("Foo()\n");
            int i = 654;

            // modification of i...
            
            DPRINT("Foo output : %d\\n", i);
            return i;
            

            }


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VisualCalc 3.0]

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #7

            you can #if 0 the code which needs to be ignored in both debug and release build, i.e. older logic to solve a problem, etc.


            -Prakash

            1 Reply Last reply
            0
            • I Imtiaz Murtaza

              I've seen in many source codes, some statements written in #if 0 block something like: #if 0 printf("Blah Blah"); #endif Please tell me what is the meaning of #if 0 in the code above.

              D Offline
              D Offline
              Divyang Mithaiwala
              wrote on last edited by
              #8

              [Message Deleted]

              M 1 Reply Last reply
              0
              • S Stephen Hewitt

                I hate #if 0:mad:. It's not so bad if the code commented out is small, but when large pieces of code are bracketed with these you can find yourself analyzing code only to find, when you get to the top, that you've been wasting your time and the code isn't even part of the product. This has happened to me many times. If the programmer simply commented it out the syntax highlighting will stop such problems from ever occurring. Steve

                K Offline
                K Offline
                kvprasad
                wrote on last edited by
                #9

                Yes steve u r definetly correct. But this is not supposed to be used to large chunks of code. it is more of an debugging approach than just saving old code. Tip: Always it is a good option to try to put a breakpoint to know if this code is compiled or not. a breakpoint cannot be inserted in any code that is not compiled. Hope this helps. kvprasad your future is what you think

                1 Reply Last reply
                0
                • D Divyang Mithaiwala

                  [Message Deleted]

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

                  :confused:


                  Maximilien Lincourt Your Head A Splode - Strong Bad

                  1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    I hate #if 0:mad:. It's not so bad if the code commented out is small, but when large pieces of code are bracketed with these you can find yourself analyzing code only to find, when you get to the top, that you've been wasting your time and the code isn't even part of the product. This has happened to me many times. If the programmer simply commented it out the syntax highlighting will stop such problems from ever occurring. Steve

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

                    Stephen Hewitt wrote:

                    If the programmer simply commented it out...

                    But nested comments are not allowed. That's where #if 0 is of use.


                    "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                    S 1 Reply Last reply
                    0
                    • D David Crow

                      Stephen Hewitt wrote:

                      If the programmer simply commented it out...

                      But nested comments are not allowed. That's where #if 0 is of use.


                      "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #12

                      True. Steve

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        I hate #if 0:mad:. It's not so bad if the code commented out is small, but when large pieces of code are bracketed with these you can find yourself analyzing code only to find, when you get to the top, that you've been wasting your time and the code isn't even part of the product. This has happened to me many times. If the programmer simply commented it out the syntax highlighting will stop such problems from ever occurring. Steve

                        P Offline
                        P Offline
                        Phil J Pearson
                        wrote on last edited by
                        #13

                        I agree that large blocks of #if 0'd code are annoying but VS2005 improves things a lot by greying it out. It even greys out (for example) #ifdef _DEBUG blocks when _DEBUG is not defined.


                        The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

                        1 Reply Last reply
                        0
                        • P Prakash Nadar

                          Imtiaz Murtaza wrote:

                          I've seen in many source codes, some statements written in #if 0 block something like: #if 0 printf("Blah Blah"); #endif Please tell me what is the meaning of #if 0 in the code above

                          it simply means the code in side the block is ignore for compilation and will not appear in the final build. it is similar to saying #if !defined(SOME_BUILD_FLAG) or #if FALSE #endif since the dev wanted the code to be explicitly removed from compilation he used #if 0 #endif directive


                          -Prakash

                          S Offline
                          S Offline
                          Shraddhan
                          wrote on last edited by
                          #14

                          I believe that in Visual C++ 6.0 there is a key combination that will take you to / from #if and #endif but Microsoft in their 'wisdom' removed this very handy functionality from VC++.NET 2001. Yes, thanks Microsoft... Personally, when I need to comment out large chunks of code, I use #if defined dfkagtljarogh #end if // defined dfkagtljarogh where dfkagtljarogh is the result of some random keystrokes. Copy and paste to make sure that two instances match, and then it is not too bad to use Find to jump from beginning to end or vice versa. And as for being unable to nest /* ... */ comments in C++, this is an abomination that should never have been put into the language. Is there any reason whatsoever why C++ (or C even) HAVE TO REFUSE to allow such useful nested comments? Thanks again, Microsoft. Some C++ compilers do allow this. Shraddhan

                          P 1 Reply Last reply
                          0
                          • S Shraddhan

                            I believe that in Visual C++ 6.0 there is a key combination that will take you to / from #if and #endif but Microsoft in their 'wisdom' removed this very handy functionality from VC++.NET 2001. Yes, thanks Microsoft... Personally, when I need to comment out large chunks of code, I use #if defined dfkagtljarogh #end if // defined dfkagtljarogh where dfkagtljarogh is the result of some random keystrokes. Copy and paste to make sure that two instances match, and then it is not too bad to use Find to jump from beginning to end or vice versa. And as for being unable to nest /* ... */ comments in C++, this is an abomination that should never have been put into the language. Is there any reason whatsoever why C++ (or C even) HAVE TO REFUSE to allow such useful nested comments? Thanks again, Microsoft. Some C++ compilers do allow this. Shraddhan

                            P Offline
                            P Offline
                            Prakash Nadar
                            wrote on last edited by
                            #15

                            Shraddhan wrote:

                            And as for being unable to nest /* ... */ comments in C++, this is an abomination that should never have been put into the language. Is there any reason whatsoever why C++ (or C even) HAVE TO REFUSE to allow such useful nested comments?

                            yes i agree with you and that is one thing that I hate in VC++ compiler.


                            -Prakash

                            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