473,748 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macro arguments

Please, consider this macro:

#define mymacro(arg1, arg2) arg1 and arg2

Then it is used:

mymacro(boys, girls)

How is its expansion?

"boys and girls" or "boys and girls" ? <-(note the double space).
Is the space character preceding the "girls" token considered?
What's the general rule?

Regards.


Jul 22 '05 #1
3 2632
John wrote:
Please, consider this macro:

#define mymacro(arg1, arg2) arg1 and arg2

Then it is used:

mymacro(boys, girls)

How is its expansion?

"boys and girls" or "boys and girls" ? <-(note the double space).
Is the space character preceding the "girls" token considered?
What's the general rule?

Regards.


Whitespace is eliminated during lexical analysis, as long as its not part of
a string, so I think this doesn't really matter.
Jul 22 '05 #2
"John" <no@spam.com> wrote in message news:<P7******* *************** @news4.tin.it>. ..
Please, consider this macro:

#define mymacro(arg1, arg2) arg1 and arg2

Then it is used:

mymacro(boys, girls)

How is its expansion?

"boys and girls" or "boys and girls" ? <-(note the double space).
Is the space character preceding the "girls" token considered?
What's the general rule?


In this particular case, I don't think it's significant. The compiler
is not going to be behaving differently due to the space being single
or double. All it does is mark the start/end of tokens. In the usual
case, you can insert as much white space as you like.

I suspect are asking this in order to get at some other issue.
For example, are you really trying to accomplish some kind of
concatenation of tokens thing? What are you trying to accomplish?
Socks
Jul 22 '05 #3
Matthias Käppler wrote:
John wrote:
Please, consider this macro:

#define mymacro(arg1, arg2) arg1 and arg2

Then it is used:

mymacro(boys, girls)

How is its expansion?

"boys and girls" or "boys and girls" ? <-(note the double space).
Is the space character preceding the "girls" token considered?
What's the general rule?

Regards.


Whitespace is eliminated during lexical analysis, as long as its not
part of a string, so I think this doesn't really matter.


No it isn't. Whitespace is significant until translation phase 7. However, the
preprocessor is allowed to compress non-newline whitespace sequences to a single
space character (see phase 3). In the case above, the whitespace has no
semantic effect even when stringized (because stringizing requires internal
whitespace to be condensed to a single space character). The only places
whitespace can matter, semantically speaking, is in the creation of string
literals or the creation of header-name preprocessing tokens. The following is
an example where the argument whitespace does change the result:

#define STR(x) #x

#define MACRO(arg) STR(+arg)

MACRO(123) // "+123"
MACRO( 123) // "+ 123"

The preprocessor is not allowed to insert whitespace anywhere, nor is allowed to
remove whitespace except in a very few specific places--in phase 3, the
preprocessor may compact whitespace; the preprocessor is required to remove
leading and trailing whitespace and condense internal whitespace when
stringizing; the preprocessor is required to remove whitespace preceding and
trailing the replacement list of a macro; and the preprocessor must ignore
whitespace between the stringizing operator (#) and its operand (i.e. # arg and
#arg are equivalent), between the token-pasting operator (##) and its operands
(i.e. a ## b and a##b, etc., are equivalent), and between the macro name (i.e.
specific identifier) and the argument list in a function-like macro invocation.

The preprocessor is not allowed to remove leading and trailing whitespace on a
macro argument, but in the above scenario, there is no semantic difference
except *maybe* in the production of an <h-char-sequence> header-name
preprocessing token, such as:

#define MACRO(arg1, arg2) <arg1 and arg2.h>

#include MACRO(boys, girls)
// <arg1 and arg2.h> or <arg1 and arg2.h> ?

However, in that case, the reinterpretatio n of a sequence to tokens produced by
macro expansion into a header-name preprocessing token is implementation
defined. As is how headers are found. So, the preprocessor is well within its
boundaries to produce either of the above results.

Regards,
Paul Mensonides
Jul 22 '05 #4

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

Similar topics

4
6264
by: Martin Magnusson | last post by:
I'm using a matrix and vector library, that won't compile. When running g++ I get the error message "macro "minor" passed 5 arguments, but takes just 1" The definition of "minor" looks like below, and it takes 3 arguments. All calls to minor that I have found in the code also pass it three arguments, so I really don't understand this error. Does anything look suspicious with the following definition, or must it be that there is some...
10
2432
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
14
4134
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to a function that would not clobber the parameter? Just surprised me a little is all.
12
6536
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x B() -nothing, *warning ISO C99 requires rest arguments to be used*
4
1858
by: Max TenEyck Woodbury | last post by:
I need a macro that will take an arbitrarily long list of arguments where each argument needs to be passed to another macro. Is it possible to write such a macro? If so, could you please provide an example?
9
321
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo(); where p is a pointer ot unsigned char.
6
2311
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in the following small example ? #include <stdio.h>
5
6298
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing directive in the middle of macro arguments, which comes handy e.g. to
5
1472
by: Peng Yu | last post by:
Hi, I want a macro #expand that can be expanded to one of the following depending on its argument f(i1) f(i1, i2) f(i1, i2, i3) ...
9
7711
by: Two-Horned Unicorn | last post by:
The following code compiles and works as expected when compiled with gcc. /* empty-args.c */ #include <stdio.h> #define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"") #define bar(x) puts("foo: x=\"" #x "\"")
0
9555
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
9376
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
9329
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
9250
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
8247
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
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
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.