473,775 Members | 3,884 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 6795

"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
10704
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
2922
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
4762
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
9458
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
10276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10111
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
7465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4023
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
3
2855
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.