473,768 Members | 5,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" ;
Nov 13 '05 #1
18 6792

"qazmlp" <qa********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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" ;


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********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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" ;


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

[snip]

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

"qazmlp" <qa********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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" ;


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*****@TAKEAW AYf2s.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******@HotPO P.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.********@att Abi.com> wrote:
"Allan Bruce" <al*****@TAKEAW AYf2s.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<<"HEL LO\n"<<std::end l ;

a valid C construct?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #7
"Alan Balmer" <al******@att.n et> wrote...
On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
<v.********@att Abi.com> wrote:
"Allan Bruce" <al*****@TAKEAW AYf2s.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<<"HEL LO\n"<<std::end l ;

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<<"HE LLO\n"<<std::en dl ;' a valid C construct.

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

>"Allan Bruce" <al*****@TAKEAW AYf2s.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<<"HEL LO\n"<<std::end l ;

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<<"H ELLO\n"<<std::e ndl ;' 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********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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" ;


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

Nov 13 '05 #10

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

Similar topics

19
1501
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. The statements like this: cout<<"something\n" ; should be made as
205
10692
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
1907
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", but I still would like to know because I am trying to encapsulate a third-party C-based callback-oriented gui toolkit inside C++ classes and I would like to try something. So say you have: long some_address = some_valid_address;
16
4504
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 following example. eg cosider 2 files sample.c and sample.h sample.h
2
6646
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 where the definitions were: _UNICODE,_DEBUG,_WIN32_DCOM,WIN32,_WINDOWS,_WINDLL,_AFXDLL,_USRDLL In VC++7.1, these are the preprocessor definitions of the project (right-click the project in Solution Explorer and choose Properties -> C++ ->...
8
2919
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 should be inserted. So, is it possible to write a macro that does this: MACRO(alpha) expands to nothing
100
4750
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 use comments. Thanks in advance! -- http://www.gotw.ca/resources/clcm.htm for info about ]
0
9577
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9412
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10022
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9969
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9847
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5427
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3938
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.