简体   繁体   中英

Different behavior in visual c++ versus gcc/clang while stringifying parameter which contains comma

I'm using stringizing operator to convert parameter which may contains comma passed to a macro into string. As I know, some characters cannot be stringified – notably, the comma(,) because it is used to delimit parameters and the right parenthesis()) because it marks the end of the parameter. So I use a variadic macro to pass commas to the stringizing operator like this:

#include <stdio.h>

#define TEST 10, 20

#define MAKE_STRING(...)  #__VA_ARGS__
#define STRING(x)       MAKE_STRING(x)

int main()
{
    printf("%s\n", STRING(TEST) );
    return 0;
}

it works fine. But it occurs to me what would happen without variadic macro, so I modify the macro: #define MAKE_STRING(x) #x . It compiles fine unexpectedly in visual c++ 2008/2010, and output 10, 20 while gcc/clang give the compilation error as expected:

macro "MAKE_STRING" passed 2 arguments, but takes just 1

So my question: is the Visual c++ doing additional work or the behavior is undefined?

VS in general allows extra parameters in macros and then just drops them silently: STRING(10, 20, 30) - still works and prints 10 . This is not the case here, but it pretty much means VS don't even have the error gcc threw at you.

It's not any additional work but "merely" a difference in substitution order.

I am not sure if this will answer your question but i hope this will help you solving your problem. When defining a string constant in C, you should include it in double quotes (for spaces). Also, the # macro wrap the variable name inside double quotes so, for example, #a become "a" .

#include <stdio.h>

#define TEST "hello, world"
#define MAKE_STRING(x) #x

int main()
{
    int a;
    printf("%s\n", TEST);
    printf("%s\n", MAKE_STRING(a));
    return 0;
}

I compiled this code using gcc 4.7.1 and the output is:

hello, world
a

I dunno why this has upvotes, or an answer got downvoted (so the poster deleted it) but I don't know what you expect!

#__VA_ARGS__ makes no sense, suppose I have MACRO(a,b,c) do you want "a,b,c" as the string?

http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros

Read, that became standard behaviour, variable length arguments in macros allow what they do in variable length arguments to functions. The pre-processor operates on text!

The only special case involving # is ##, which deletes a comma before the ## if there are no extra arguments (thus preventing a syntax error)

NOTE:

It is really important you read the MACRO(a,b,c) part and what do you expect, a string "a,b,c"? or "a, b, c" if you want the string "a, b, c" WRITE THE STRING "a, b, c"

Using the # operator is great for stuff like

#define REGISTER_THING(THING) core_of_program.register_thing(THING); printf("%s registered\n",#THING);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM