Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > use of #define MACRO(A,B)

Reply
Thread Tools

use of #define MACRO(A,B)

 
 
Tagore
Guest
Posts: n/a
 
      03-31-2010
hi,

While understanding some code, I came across a definition of macro
without expansion
#define MACRO(A,B) // expansion missing

What could be use of such macro?

I think, there is no use of it without expanding e.g.
#define MACRO(A,B) A+B // expansion given

Please clear doubts

Thanks,

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      03-31-2010
Tagore <(E-Mail Removed)> wrote:
> While understanding some code, I came across a definition
> of macro without expansion
> #define MACRO(A,B) * * *// expansion missing
>
> What could be use of such macro?


To produce nothing in certain circumstances.

> I think, there is no use of it without expanding e.g.
> #define MACRO(A,B) A+B * *// expansion given
>
> Please clear doubts


#if NDEBUG
#define DEBUG(a,b)
#else
#define DEBUG(a,b) fprintf(stderr, "%s: %d\n", a, (int) (b))
#endif

DEBUG("Foo()", 42);

--
Peter
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      03-31-2010
On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
<(E-Mail Removed)> wrote:

>hi,
>
>While understanding some code, I came across a definition of macro
>without expansion
>#define MACRO(A,B) // expansion missing


I hope the comment is your addition and not present in the source.

>
>What could be use of such macro?
>
>I think, there is no use of it without expanding e.g.
>#define MACRO(A,B) A+B // expansion given
>
>Please clear doubts


At one time early in the development, it could have been a debugging
macro of the form
#define MACRO(A,B) \
printf(#A "=%d, " #B "=%d.\n", A, B)

After the developer was satisfied, the new definition reduces any
invocations of MACO to white space which should be ignored during
compilation.

--
Remove del for email
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      03-31-2010
On 2010-03-31, Tagore <(E-Mail Removed)> wrote:
> While understanding some code, I came across a definition of macro
> without expansion
> #define MACRO(A,B) // expansion missing


> What could be use of such macro?


To make calls of the form "MACRO(arg1, arg2)" turn into nothing.

For instance, consider

#ifdef VERBOSE
#define DEBUG(x) x
#else
#define DEBUG(x)
#endif

DEBUG(printf("%d\n", foo->bar));

If VERBOSE is defined, this expands to printf, otherwise to an empty
statement which does nothing.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      03-31-2010
Barry Schwarz wrote:
> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
> <(E-Mail Removed)> wrote:


>> While understanding some code, I came across a definition of macro
>> without expansion
>> #define MACRO(A,B) // expansion missing

>
> I hope the comment is your addition and not present in the source.


While I typically don't use // comments, I have otherwise written macros with a
commented whitespace-only body, perhaps
#define MACRO(A,B) /* nothing */

It documented that the macro replacement was intentionally empty.

--
Thad
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      04-01-2010
On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <(E-Mail Removed)>
wrote:

>Barry Schwarz wrote:
>> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
>> <(E-Mail Removed)> wrote:

>
>>> While understanding some code, I came across a definition of macro
>>> without expansion
>>> #define MACRO(A,B) // expansion missing

>>
>> I hope the comment is your addition and not present in the source.

>
>While I typically don't use // comments, I have otherwise written macros with a
>commented whitespace-only body, perhaps
>#define MACRO(A,B) /* nothing */
>
>It documented that the macro replacement was intentionally empty.


But your comment is closed, so a statement of the form
if (x)
MACRO(y,z);
compiles as
if (x)
/* nothing */ ;
while the code in the OP will compile as
if (x)
// expansion missing ;
which will do something unintended with the next statement.

--
Remove del for email
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-01-2010
Barry Schwarz <(E-Mail Removed)> writes:

> On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <(E-Mail Removed)>
> wrote:
>
>>Barry Schwarz wrote:
>>> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
>>> <(E-Mail Removed)> wrote:

>>
>>>> While understanding some code, I came across a definition of macro
>>>> without expansion
>>>> #define MACRO(A,B) // expansion missing
>>>
>>> I hope the comment is your addition and not present in the source.

>>
>>While I typically don't use // comments, I have otherwise written macros with a
>>commented whitespace-only body, perhaps
>>#define MACRO(A,B) /* nothing */
>>
>>It documented that the macro replacement was intentionally empty.

>
> But your comment is closed, so a statement of the form
> if (x)
> MACRO(y,z);
> compiles as
> if (x)
> /* nothing */ ;
> while the code in the OP will compile as
> if (x)
> // expansion missing ;
> which will do something unintended with the next statement.


This is not right. Comments are removed (well, replaced with a single
space) in translation phase 3 before pre-processing is done. Both
definitions of MACRO are empty.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments