commenting out 'cout' using preprocessor macro

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qazmlp

    commenting out 'cout' using preprocessor macro

    I hope comp.lang.c will not find the following question as a
    complete off-topic.

    I would like to remove ie.comment out the 'cout' statements during
    compilation(act ually preprocessing) time.


    The statements like this:
    cout<<"somethin g\n" ;
    should be made as
    // cout<<"somethin g\n" ;


    I tried for the following. But, It doesn't seem to be working.


    //--------START
    #ifdef DEBUG
    #define COUT std::cout
    #else
    #define COUT \/\/
    #endif

    int main()
    {
    COUT<<"HELLO\n" <<std::endl ;
    }

    //--------END


    If you can solve the above problem, please suggest a way for taking
    care of
    commenting out the 'cout' statements that spans in more than 1 line.
    eg:

    12 COUT<<"HELLO\n"
    13 <<"WORLD\n" ;
  • Allan Bruce

    #2
    Re: commenting out 'cout' using preprocessor macro


    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
    news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=blue]
    > I hope comp.lang.c will not find the following question as a
    > complete off-topic.
    >
    > I would like to remove ie.comment out the 'cout' statements during
    > compilation(act ually preprocessing) time.
    >
    >
    > The statements like this:
    > cout<<"somethin g\n" ;
    > should be made as
    > // cout<<"somethin g\n" ;
    >
    >
    > I tried for the following. But, It doesn't seem to be working.
    >
    >
    > //--------START
    > #ifdef DEBUG
    > #define COUT std::cout
    > #else
    > #define COUT \/\/
    > #endif
    >
    > int main()
    > {
    > COUT<<"HELLO\n" <<std::endl ;
    > }
    >
    > //--------END
    >
    >
    > If you can solve the above problem, please suggest a way for taking
    > care of
    > commenting out the 'cout' statements that spans in more than 1 line.
    > eg:
    >
    > 12 COUT<<"HELLO\n"
    > 13 <<"WORLD\n" ;[/color]

    This is most definately a c++ question, but the same concept works in c.
    The preprocessor directly copies across what you have in a define, so why
    not just have
    #define COUT //
    Allan


    Comment

    • Jeff

      #3
      Re: commenting out 'cout' using preprocessor macro


      "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
      news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=blue]
      > I hope comp.lang.c will not find the following question as a
      > complete off-topic.
      >
      > I would like to remove ie.comment out the 'cout' statements during
      > compilation(act ually preprocessing) time.
      >
      >
      > The statements like this:
      > cout<<"somethin g\n" ;
      > should be made as
      > // cout<<"somethin g\n" ;
      >
      >[/color]

      Sorry, it is off-topic here. cout is a part of C++, not C.

      [snip]

      --
      Jeff


      Comment

      • Victor Bazarov

        #4
        Re: commenting out 'cout' using preprocessor macro

        "Jeff" <nobody@notexis t.com> wrote...[color=blue]
        >
        > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
        > news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=green]
        > > I hope comp.lang.c will not find the following question as a
        > > complete off-topic.
        > >
        > > I would like to remove ie.comment out the 'cout' statements during
        > > compilation(act ually preprocessing) time.
        > >
        > >
        > > The statements like this:
        > > cout<<"somethin g\n" ;
        > > should be made as
        > > // cout<<"somethin g\n" ;
        > >
        > >[/color]
        >
        > Sorry, it is off-topic here. cout is a part of C++, not C.[/color]

        Sorry, Jeff, statements explaining what is off-topic in
        comp.lang.c are off-topic in comp.lang.c++. And give "qazmlp"
        a break, he expressed his hope, didn't he?

        Victor


        Comment

        • Victor Bazarov

          #5
          Re: commenting out 'cout' using preprocessor macro

          "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote...[color=blue]
          > [...]
          > This is most definately a c++ question, but the same concept works in c.[/color]

          Why is it "definately " a C++ question? C has end-of-line comments.
          Depending on how 'cout' is declared, it can be seen as a valid C
          construct as well (just as in C++ I can declare it whatever I want
          without including the <iostream>).. .
          [color=blue]
          > The preprocessor directly copies across what you have in a define, so why
          > not just have
          > #define COUT //[/color]

          Why not? Simple. Comments are replaced with a single space char
          before macro processing is ever begins. So, the directive you
          wrote will be

          #define COUT

          after the phase 3 of the translation in both languages.

          Victor


          Comment

          • Victor Bazarov

            #6
            Re: commenting out 'cout' using preprocessor macro

            "Derk Gwen" <derkgwen@HotPO P.com> wrote...[color=blue]
            > # //--------START
            > # #ifdef DEBUG
            > # #define COUT std::cout
            > # #else
            > # #define COUT \/\/
            > # #endif
            >
            > How about
            > #ifdef DEBUG
            > #define COUT if (1) std::cout
            > #else
            > #define COUT if (0) std::cout
            > #endif
            >
            > If you optimise, the unexecutable code after if (0) should be excised.
            >
            > # 12 COUT<<"HELLO\n"
            > # 13 <<"WORLD\n" ;
            >
            > if (1) std::cout <<"HELLO\n"
            > <<"WORLD\n" ;
            >
            > if (0) std::cout <<"HELLO\n"
            > <<"WORLD\n" ;[/color]

            This approach has a major flaw. Imagine what this will expand into

            if (somecondition)
            COUT << "HELLO";
            else
            puts("condition is not met");

            This is why it's better to use 'while' for that:

            #ifdef WHATEVER
            #define COUT std::cout
            #else
            #define COUT while(0) std::cout
            #endif

            However, that doesn't address the OP's concern that the code still
            remains not compileable by a C compiler. It would be much better
            to remove any reference to 'std::cout' whatsoever.

            #ifdef __cplusplus
            #define COUT std::cout
            #else
            #define COUT ????
            #endif

            I don't have a solution. The biggest problem in such case is how
            you deal with user-define types that can be output using the C++
            shift operator:

            SomeUDT udt;
            std::cout << udt; // is not uncommon in C++ programs

            Making the whole statement invisible to the compiler (after the
            preprocessing stage) is the task at hand (or at least how I see
            it)...

            Victor


            Comment

            • Alan Balmer

              #7
              Re: commenting out 'cout' using preprocessor macro

              On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
              <v.Abazarov@att Abi.com> wrote:
              [color=blue]
              >"Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote...[color=green]
              >> [...]
              >> This is most definately a c++ question, but the same concept works in c.[/color]
              >
              >Why is it "definately " a C++ question? C has end-of-line comments.
              >Depending on how 'cout' is declared, it can be seen as a valid C
              >construct as well (just as in C++ I can declare it whatever I want
              >without including the <iostream>).. .[/color]

              I'm curious - just how would you declare 'cout" to make

              std::cout<<"HEL LO\n"<<std::end l ;

              a valid C construct?

              --
              Al Balmer
              Balmer Consulting
              removebalmercon sultingthis@att .net

              Comment

              • Victor Bazarov

                #8
                Re: commenting out 'cout' using preprocessor macro

                "Alan Balmer" <albalmer@att.n et> wrote...[color=blue]
                > On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
                > <v.Abazarov@att Abi.com> wrote:
                >[color=green]
                > >"Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote...[color=darkred]
                > >> [...]
                > >> This is most definately a c++ question, but the same concept works in[/color][/color][/color]
                c.[color=blue][color=green]
                > >
                > >Why is it "definately " a C++ question? C has end-of-line comments.
                > >Depending on how 'cout' is declared, it can be seen as a valid C
                > >construct as well (just as in C++ I can declare it whatever I want
                > >without including the <iostream>).. .[/color]
                >
                > I'm curious - just how would you declare 'cout" to make
                >
                > std::cout<<"HEL LO\n"<<std::end l ;
                >
                > a valid C construct?[/color]

                I didn't say that the entire statement is a valid construct.
                I said 'cout' could be a valid construct. To make 'cout' valid
                all you need to do is

                int cout;

                In the context of the thread there was no requirement to make
                'std::cout<<"HE LLO\n"<<std::en dl ;' a valid C construct.

                Victor


                Comment

                • Victor Bazarov

                  #9
                  Re: commenting out 'cout' using preprocessor macro

                  "Alan Balmer" <albalmer@att.n et> wrote...[color=blue]
                  > On Fri, 8 Aug 2003 13:40:58 -0400, "Victor Bazarov"
                  > <v.Abazarov@att Abi.com> wrote:
                  >[color=green]
                  > >"Alan Balmer" <albalmer@att.n et> wrote...[color=darkred]
                  > >> On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
                  > >> <v.Abazarov@att Abi.com> wrote:
                  > >>
                  > >> >"Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote...
                  > >> >> [...]
                  > >> >> This is most definately a c++ question, but the same concept works[/color][/color][/color]
                  in[color=blue][color=green]
                  > >c.[color=darkred]
                  > >> >
                  > >> >Why is it "definately " a C++ question? C has end-of-line comments.
                  > >> >Depending on how 'cout' is declared, it can be seen as a valid C
                  > >> >construct as well (just as in C++ I can declare it whatever I want
                  > >> >without including the <iostream>).. .
                  > >>
                  > >> I'm curious - just how would you declare 'cout" to make
                  > >>
                  > >> std::cout<<"HEL LO\n"<<std::end l ;
                  > >>
                  > >> a valid C construct?[/color]
                  > >
                  > >I didn't say that the entire statement is a valid construct.
                  > >I said 'cout' could be a valid construct. To make 'cout' valid
                  > >all you need to do is
                  > >
                  > > int cout;
                  > >
                  > >In the context of the thread there was no requirement to make
                  > >'std::cout<<"H ELLO\n"<<std::e ndl ;' a valid C construct.
                  > >
                  > >Victor
                  > >[/color]
                  > The context of the thread was that you were contesting the statement
                  > that the OP's question concerned C++.[/color]

                  No, I wasn't. That's something you just invented. The OP's question
                  concerns both C++ and C. Off-topicality of the OP's question in
                  comp.lang.c is what I was contesting.

                  Victor


                  Comment

                  • Samuele Armondi

                    #10
                    Re: commenting out 'cout' using preprocessor macro

                    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
                    news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=blue]
                    > I hope comp.lang.c will not find the following question as a
                    > complete off-topic.
                    >
                    > I would like to remove ie.comment out the 'cout' statements during
                    > compilation(act ually preprocessing) time.
                    >
                    >
                    > The statements like this:
                    > cout<<"somethin g\n" ;
                    > should be made as
                    > // cout<<"somethin g\n" ;
                    >
                    >
                    > I tried for the following. But, It doesn't seem to be working.
                    >
                    >
                    > //--------START
                    > #ifdef DEBUG
                    > #define COUT std::cout
                    > #else
                    > #define COUT \/\/
                    > #endif
                    >
                    > int main()
                    > {
                    > COUT<<"HELLO\n" <<std::endl ;
                    > }
                    >
                    > //--------END
                    >
                    >
                    > If you can solve the above problem, please suggest a way for taking
                    > care of
                    > commenting out the 'cout' statements that spans in more than 1 line.
                    > eg:
                    >
                    > 12 COUT<<"HELLO\n"
                    > 13 <<"WORLD\n" ;[/color]

                    Maybe you could create your own stream and use that as cout, i.e
                    #if !defined(DEBUG) && defined(__cplus plus)
                    class MyStream : public std::ostream \
                    { \
                    template <class T> \
                    MyStream& operator << (const T& obj) \
                    { return *this; } \
                    }; \
                    #define COUT MyStream
                    #else
                    #define COUT std::cout
                    #endif
                    I'm not sure if the above code will work, I'm especially not too sure about
                    the template bit. And it most definitely would not work on c systems! Maybe
                    you could define cout to be an int and the << operator to be + , i.e
                    #if !defined(DEBUG) && !defined(__cplu splus)
                    int bogus_cout;
                    #define COUT bogus_cout=
                    #define << +
                    #elif !defined(DEBUG) && defined(__cplus plus)
                    class MyStream : public std::ostream \
                    { \
                    template <class T> \
                    MyStream& operator << (const T& obj) \
                    { return *this; } \
                    }; \
                    #define COUT MyStream
                    #else
                    #define COUT std::cout
                    #endif
                    But you would need to be 100% sure that the code in question does not use
                    the << _anywhere_. Hope this helps anyway!
                    S. Armondi



                    Comment

                    • Samuele Armondi

                      #11
                      Re: commenting out 'cout' using preprocessor macro


                      "Samuele Armondi" <sammyboyuk_NOS PAM_@hotmail.co m> wrote in message
                      news:3f3425c8_2 @mk-nntp-1.news.uk.world online.com...[color=blue]
                      > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
                      > news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=green]
                      > > I hope comp.lang.c will not find the following question as a
                      > > complete off-topic.[/color][/color]
                      [snip...][color=blue]
                      > But you would need to be 100% sure that the code in question does not use
                      > the << _anywhere_. Hope this helps anyway!
                      > S. Armondi
                      >[/color]
                      Sorry, ignore all the \ in the declaration of the MyStream class. They are
                      not needed.
                      S. Armondi


                      Comment

                      • Bill Cunningham

                        #12
                        Re: commenting out 'cout' using preprocessor macro


                        [color=blue]
                        > after the phase 3 of the translation in both languages.
                        >
                        > Victor[/color]

                        What exactly is phase 3 translation? Into assembly or RTL perhaps?

                        Bill





                        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                        -----== Over 80,000 Newsgroups - 16 Different Servers! =-----

                        Comment

                        • Jeff

                          #13
                          Re: commenting out 'cout' using preprocessor macro


                          "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:vj7fqgdgtl l074@corp.super news.com...[color=blue]
                          > "Jeff" <nobody@notexis t.com> wrote...[color=green]
                          > >
                          > > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
                          > > news:db9bbf31.0 308080137.7c34c 41e@posting.goo gle.com...[color=darkred]
                          > > > I hope comp.lang.c will not find the following question as a
                          > > > complete off-topic.
                          > > >
                          > > > I would like to remove ie.comment out the 'cout' statements during
                          > > > compilation(act ually preprocessing) time.
                          > > >
                          > > >
                          > > > The statements like this:
                          > > > cout<<"somethin g\n" ;
                          > > > should be made as
                          > > > // cout<<"somethin g\n" ;
                          > > >
                          > > >[/color]
                          > >
                          > > Sorry, it is off-topic here. cout is a part of C++, not C.[/color]
                          >
                          > Sorry, Jeff, statements explaining what is off-topic in
                          > comp.lang.c are off-topic in comp.lang.c++. And give "qazmlp"
                          > a break, he expressed his hope, didn't he?
                          >[/color]

                          Sorry, Victor, statements explaining the other is off-topic here is also off-topic.

                          Unfortunately, you are off-topic now.

                          (Just to remind you, we have the right to protect our c.l.c group from off-topic by kindly notifying
                          the OP. It is usual on Usenet and I am not the first one. If you insist that the OP is not off-topic
                          here, you can give your opinion)
                          [color=blue]
                          > Victor
                          >
                          >[/color]

                          --
                          Jeff


                          Comment

                          • Jack Klein

                            #14
                            Re: commenting out 'cout' using preprocessor macro

                            On Fri, 8 Aug 2003 23:57:54 -0400, "Bill Cunningham" <some@some.ne t>
                            wrote in comp.lang.c:
                            [color=blue]
                            >
                            >[color=green]
                            > > after the phase 3 of the translation in both languages.
                            > >
                            > > Victor[/color]
                            >
                            > What exactly is phase 3 translation? Into assembly or RTL perhaps?
                            >
                            > Bill[/color]

                            The original ANSI/ISO C language standard (1989/1990) provided a
                            high-level definition of the translation process that an
                            implementation performs in producing executable code from a source
                            file. These have persisted to the present in later versions of the C
                            standard and have been incorporated into the C++ standard.

                            Like everything else in C and C++, the "as-if" rule applies, meaning
                            that a compiler does not have use separate passes to perform all of
                            these operations. It can do them in parallel as long as the proper
                            ordering is used.

                            They are too long to quote here in full, but the two that are relevant
                            to this question are that in phase 3, "Each comment is replaced by one
                            space character", and in phase 4, "Preprocess ing directives are
                            executed and macro invocations are expanded".

                            So there is no way of including a comment in the expansion of a macro.
                            The early lexical analysis step removes comments before the
                            preprocessor expands macros.

                            An overview of the 8 phases of translation:

                            1 through 6 deals with parsing and preprocessor issues. At the end of
                            phase 6 the program is a series of tokens, without white space, with
                            all comments removed and macros expanded.

                            Phase 7 actually analyses and translates the program into some sort of
                            output format, generally referred to as an object file.

                            Phase 8 is after compilation, usually performed by a separate tool
                            called a linker. It resolves references between different source
                            files and to library modules and generally produces the final
                            executable output file.

                            --
                            Jack Klein
                            Home: http://JK-Technology.Com
                            FAQs for
                            comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                            alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                            Comment

                            • Victor Bazarov

                              #15
                              Re: commenting out 'cout' using preprocessor macro

                              "Jeff" <nobody@notexis t.com> wrote...[color=blue]
                              > [...]
                              > Sorry, Victor, statements explaining the other is off-topic here is also[/color]
                              off-topic.

                              REALLY?
                              [color=blue]
                              > Unfortunately, you are off-topic now.
                              >
                              > (Just to remind you, we have the right to protect our c.l.c group from[/color]
                              off-topic by kindly notifying[color=blue]
                              > the OP. It is usual on Usenet and I am not the first one. If you insist[/color]
                              that the OP is not off-topic[color=blue]
                              > here, you can give your opinion)[/color]

                              I already have. If you need it again, go to my first reply to you
                              and re-read it.


                              Comment

                              Working...