1

I found the below macro in an SDK sample program. What #value is meant for in this context?

#define VALUE_CHAR(value)       #value, value
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Phoenix
  • 43
  • 5

2 Answers2

7

Stringification. See this page.

So

VALUE_CHAR(1)

expands to:

"1", 1

You might use this kind of macro to simplify initialization of an array, for example:

#define MYDEF(x) { #x, x }

static struct {
    const char *str;
    int num;
} values[] = {
    MYDEF(1),
    MYDEF(2),
    MYDEF(3)
};
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @trojanfoe: Do we need to have `#value, value`? Because I tried with `#value` it did work. then why `value` need to be written twice – Rasmi Ranjan Nayak Aug 29 '13 at 14:18
  • @RasmiRanjanNayak It depends on what that particular macro means to the API/framework you are using. – trojanfoe Aug 29 '13 at 14:19
  • 2
    @RasmiRanjanNayak: In this example, each array initialiser expands to e.g. `{ "1", 1 }`, to initialise both the string and the integer to matching values. With just `#value` it would "work" (in the sense of compiling), but would leave the integer uninitialised. – Mike Seymour Aug 29 '13 at 14:23
  • @trojanfoe: I am just curious to know how to create object of this structure in ur example above. This is a very good coding. Because I tried with `struct values val;` and `struct values[0] val;` But it is throwing error. Please help me. – Rasmi Ranjan Nayak Aug 29 '13 at 14:24
  • 1
    @RasmiRanjanNayak Well we are departing from your question, but in my example the struct is *anonymous* (it has no type as such). If you want to use it elsewhere then `struct myvalue { const char *str; int num; };` and then `static struct myvalue values[] = { ... };`. etc. – trojanfoe Aug 29 '13 at 14:27
3

From the standard :

16.3.2 The # operator [cpp.stringize]

A character string literal is a string-literal with no prefix. If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument.

It means that:

#define VALUE_CHAR(value)    #value, value
VALUE_CHAR(some_value)

Will be expanded to :

"some_value", some_value

by the preprocessor.


For example, the famous BOOST Library uses this operator to stringize token :

#define BOOST_STRINGIZE(X) BOOST_DO_STRINGIZE(X)
#define BOOST_DO_STRINGIZE(X) #X

An example of the usage in the Test library:

#define BOOST_AUTO_TEST_SUITE( suite_name )                             \
namespace suite_name {                                                  \
  TheFunction( BOOST_STRINGIZE( suite_name ) );                         \
//             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

// ...

BOOST_AUTO_TEST_SUITE( MyTest );

Will be expanded to:

namespace MyTest {
    TheFunction( "MyTest" );
//               ^^^^^^^^
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62