473,320 Members | 2,054 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,320 software developers and data experts.

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 2585
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 reinterpretation 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
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...
10
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
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...
12
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 ...
4
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...
9
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();...
6
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...
5
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...
5
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
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) ...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.