473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\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" ;
Nov 13 '05 #1
18 6741

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\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" ;


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
Nov 13 '05 #2

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\n" ;


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

[snip]

--
Jeff
Nov 13 '05 #3
"Jeff" <no****@notexist.com> wrote...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\n" ;


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


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
Nov 13 '05 #4
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote...
[...]
This is most definately a c++ question, but the same concept works in c.
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>)...
The preprocessor directly copies across what you have in a define, so why
not just have
#define COUT //


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
Nov 13 '05 #5
"Derk Gwen" <de******@HotPOP.com> wrote...
# //--------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" ;


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
Nov 13 '05 #6
On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote...
[...]
This is most definately a c++ question, but the same concept works in c.


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<<"HELLO\n"<<std::endl ;

a valid C construct?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #7
"Alan Balmer" <al******@att.net> wrote...
On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote...
[...]
This is most definately a c++ question, but the same concept works in
c.
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<<"HELLO\n"<<std::endl ;

a valid C construct?


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<<"HELLO\n"<<std::endl ;' a valid C construct.

Victor
Nov 13 '05 #8
"Alan Balmer" <al******@att.net> wrote...
On Fri, 8 Aug 2003 13:40:58 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Alan Balmer" <al******@att.net> wrote...
On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:

>"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote...
>> [...]
>> This is most definately a c++ question, but the same concept works
inc.
>
>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<<"HELLO\n"<<std::endl ;

a valid C construct?


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<<"HELLO\n"<<std::endl ;' a valid C construct.

Victor

The context of the thread was that you were contesting the statement
that the OP's question concerned C++.


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
Nov 13 '05 #9
"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\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" ;


Maybe you could create your own stream and use that as cout, i.e
#if !defined(DEBUG) && defined(__cplusplus)
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(__cplusplus)
int bogus_cout;
#define COUT bogus_cout=
#define << +
#elif !defined(DEBUG) && defined(__cplusplus)
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

Nov 13 '05 #10

"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com...
"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
I hope comp.lang.c will not find the following question as a
complete off-topic.
[snip...] But you would need to be 100% sure that the code in question does not use
the << _anywhere_. Hope this helps anyway!
S. Armondi

Sorry, ignore all the \ in the declaration of the MyStream class. They are
not needed.
S. Armondi
Nov 13 '05 #11

after the phase 3 of the translation in both languages.

Victor


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! =-----
Nov 13 '05 #12

"Victor Bazarov" <v.********@attAbi.com> wrote in message news:vj************@corp.supernews.com...
"Jeff" <no****@notexist.com> wrote...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\n" ;
Sorry, it is off-topic here. cout is a part of C++, not C.


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?


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)
Victor


--
Jeff
Nov 13 '05 #13
On Fri, 8 Aug 2003 23:57:54 -0400, "Bill Cunningham" <so**@some.net>
wrote in comp.lang.c:

after the phase 3 of the translation in both languages.

Victor


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

Bill


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, "Preprocessing 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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #14
"Jeff" <no****@notexist.com> wrote...
[...]
Sorry, Victor, statements explaining the other is off-topic here is also off-topic.

REALLY?
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)


I already have. If you need it again, go to my first reply to you
and re-read it.
Nov 13 '05 #15
qazmlp wrote:
I would like to remove ie.comment out the 'cout' statements during
compilation(actually preprocessing) time.


I saw this in a computer mag once (either C++ journal or something like
that):

#ifdef DEBUG
#define SLASH(s) /##s
#define COMMENT SLASH(/)
#else
#define COMMENT std::cout
#endif

Unfortunately, I don't know how portable this is (someone could probably
explain why it won't work). I could get it working on VisualC++ v6, but
had problems with GCC v2.9x. Think it worked in aCC (the HPUX compiler)

--
John L. Fjellstad
Nov 13 '05 #16
John L Fjellstad <jo*******@fjellstad.org> writes:
I saw this in a computer mag once (either C++ journal or something like
that):

#ifdef DEBUG
#define SLASH(s) /##s
#define COMMENT SLASH(/)
#else
#define COMMENT std::cout
#endif

Unfortunately, I don't know how portable this is (someone could probably
explain why it won't work).


Comments are removed (or rather replaced by a whitespace character) in
translation phase 3, while preprocessing directives are executed and macros
are expanded in translation phase 4. Therefore, `//' is not a meaningful
preprocessing token at the point when `COMMENT' is expanded.

Martin
Nov 13 '05 #17
Martin Dickopp wrote:
Comments are removed (or rather replaced by a whitespace character) in
translation phase 3, while preprocessing directives are executed and
macros are expanded in translation phase 4. Therefore, `//' is not a
meaningful preprocessing token at the point when `COMMENT' is expanded.


Do you know if this is in the C++ standard, or if it is left to the
implementation?

--
John L. Fjellstad
Nov 13 '05 #18
John L Fjellstad <jo*******@fjellstad.org> writes:
Martin Dickopp wrote:
Comments are removed (or rather replaced by a whitespace character) in
translation phase 3, while preprocessing directives are executed and
macros are expanded in translation phase 4. Therefore, `//' is not a
meaningful preprocessing token at the point when `COMMENT' is expanded.


Do you know if this is in the C++ standard, or if it is left to the
implementation?


This behavior is required by the C++ standard (section 2.1#1) as well as the
C standard (section 5.1.1.2#1).

Martin
(Followup-To: comp.lang.c++ ignored, since the discussion seems meaningful
to both C and C++.)
Nov 13 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
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(actually preprocessing) time. ...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
8
by: claus.tondering | last post by:
I need to write a macro that inserts someStruct m_someStruct; into another struct declaration. The problem is that if the programmer specifies one particluar struct (called alpha), nothing...
100
by: Angel Tsankov | last post by:
Can someone recommend a good source of C/C++ coding style. Specifically, I am interested in commenting style and in particular how to indent comments and the commented code, rather than when to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.