Preprocessor: Textual value of a macro 
Author Message
 Preprocessor: Textual value of a macro

How can one get the text of a macro's value into a string?

Suppose that the macro BIT64TYPE is defined, either as a #define
(in an include file somewhere), e.g.:
    #define BIT64TYPE   int64_t
or as a -D option to the compiler: -D BIT64TYPE=int64_t

Now how do I get that value into a string?  That is, I'd want to:
    fprintf(outfile, "extern <<BIT64TYPE>> bignum; \n");
where <<BIT64TYPE>> represents whatever I do to get "int64_t" to
appear at that point in the string.

The C preprocessor stringization macro # doesn't actually do this.
It seems to apply before the value of the macro is substituted.
So, preprocessing the following file:
   #define BIT64TYPE int64_t
   #define STRINGIFY(x) #x
   char * s = STRINGIFY(BIT64TYPE)
yields:
   char * s = "BIT64TYPE"
not:
   char * s = "int64_t"

What I want is reasonable.  But is it possible?  Are there C
preprocessor techniques that can do the job?

- David Librik

--



Sun, 28 Nov 2004 04:54:26 GMT  
 Preprocessor: Textual value of a macro

Quote:
>How can one get the text of a macro's value into a string?

>Suppose that the macro BIT64TYPE is defined, either as a #define
>(in an include file somewhere), e.g.:
>    #define BIT64TYPE   int64_t
>or as a -D option to the compiler: -D BIT64TYPE=int64_t

>Now how do I get that value into a string?  That is, I'd want to:
>    fprintf(outfile, "extern <<BIT64TYPE>> bignum; \n");
>where <<BIT64TYPE>> represents whatever I do to get "int64_t" to
>appear at that point in the string.

You need one more level of expansion.  E.g.:

   C:\Dave>type expand.c
   #include <stdio.h>

   #define BIT64TYPE int64_t

   #define mSTRINGIFY(x) #x
   #define mEXPAND(x) mSTRINGIFY(x)

   int main(void)
   {
      puts(mEXPAND(BIT64TYPE));
      return 0;
   }

   C:\Dave>gcc expand.c

   C:\Dave>a.exe
   int64_t

Regards,

                               -=Dave
--
Change is inevitable, progress is not.
--



Mon, 29 Nov 2004 01:59:26 GMT  
 Preprocessor: Textual value of a macro

Quote:
> How can one get the text of a macro's value into a string?

This is in the comp.lang.c FAQ; you need to introduce a level of
indirection to force the macro to be evaluated before adding quotes:

#define STRING(x) STR(x)
#define STR(x) #x

char *var = STRING(SOME_MACRO);

--
Eric Amick
Columbia, MD
--



Mon, 29 Nov 2004 01:59:34 GMT  
 Preprocessor: Textual value of a macro


Quote:
>How can one get the text of a macro's value into a string?

>Suppose that the macro BIT64TYPE is defined, either as a #define
>(in an include file somewhere), e.g.:
>    #define BIT64TYPE   int64_t
>or as a -D option to the compiler: -D BIT64TYPE=int64_t

>Now how do I get that value into a string?  That is, I'd want to:
>    fprintf(outfile, "extern <<BIT64TYPE>> bignum; \n");
>where <<BIT64TYPE>> represents whatever I do to get "int64_t" to
>appear at that point in the string.

>The C preprocessor stringization macro # doesn't actually do this.
>It seems to apply before the value of the macro is substituted.
>So, preprocessing the following file:
>   #define BIT64TYPE int64_t
>   #define STRINGIFY(x) #x
>   char * s = STRINGIFY(BIT64TYPE)
>yields:
>   char * s = "BIT64TYPE"
>not:
>   char * s = "int64_t"

>What I want is reasonable.  But is it possible?  Are there C
>preprocessor techniques that can do the job?

/* str.c */
#define BIT64TYPE       int64_t
#define STRINGIFY(x)    #x
#define STRINGIND(x)    STRINGIFY(x)
char *s         = STRINGIND(BIT64TYPE);

$ cc -E -P str.c

char *s         = "int64_t"  ;

--

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada


    fake address                use address above to reply


                                                spam traps
--



Mon, 29 Nov 2004 01:59:56 GMT  
 Preprocessor: Textual value of a macro
in comp.lang.c.moderated i read:

Quote:
>How can one get the text of a macro's value into a string?

#define STR(x) #x
#define STRVAL(x) STR(x)

puts( STR(INT_MAX) )    -> INT_MAX
puts( STRVAL(INT_MAX) ) -> some value, e.g., 2147483647

--
bringing you boring signatures for 17 years
--



Mon, 29 Nov 2004 02:05:13 GMT  
 Preprocessor: Textual value of a macro

Quote:

> How can one get the text of a macro's value into a string?
> The C preprocessor stringization macro # doesn't actually do this.

It does, if used a bit more indirectly.  Try

     #define BIT64TYPE int64_t
     #define STRINGIFY(x) #x
     #define STRINGIFYVALUE(x) STRINGIFY(x)
     char * s = STRINGIFYVALUE(BIT64TYPE);

The trick is to nest two levels of macro expansion: one to replace the
macro name by its value, the second to stringize that.
--

Even if all the snow were burnt, ashes would remain.
--



Mon, 29 Nov 2004 02:05:22 GMT  
 Preprocessor: Textual value of a macro

Quote:
>How can one get the text of a macro's value into a string?

>Suppose that the macro BIT64TYPE is defined, either as a #define
>(in an include file somewhere), e.g.:
>    #define BIT64TYPE   int64_t
>or as a -D option to the compiler: -D BIT64TYPE=int64_t

>Now how do I get that value into a string?

By reading the FAQ.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Mon, 29 Nov 2004 02:05:20 GMT  
 Preprocessor: Textual value of a macro

Quote:

>    #define BIT64TYPE int64_t
>    #define STRINGIFY(x) #x
>    char * s = STRINGIFY(BIT64TYPE)

The usual method is to make yet another macro:

  #define STRINGIFY2(x) STRINGIFY(x)

Then, STRINGIFY2(BIT64TYPE) expands to "int64_t".  You might swap
the names of STRINGIFY and STRINGIFY2 if you use the latter more.

I've used this technique for generating format strings for edit
boxes in a user-interface library.  For example, I wanted an edit
box that would accept a string of at most USER_NAME_LEN
characters, and USER_NAME_LEN was #defined as 20.  I would then
generate the string as STRINGIFY2(USER_NAME_LEN)"X".  This would
expand to "20""X", which the compiler would concatenate to "20X",
and the edit box would allow 20 arbitrary characters.

It once happened that I forgot to #include the header where
USER_NAME_LEN was defined, and the format string became
"USER_NAME_LENX"... the result did not look particularly pretty.
Nowadays, the program has plenty of #ifndef/#error/#endif checks.

(With printf, one could instead use "%.*s" and pass the maximum
length as a separate parameter, but the edit-box code did not
support that.)
--



Mon, 29 Nov 2004 23:22:15 GMT  
 Preprocessor: Textual value of a macro

Quote:



>> How can one get the text of a macro's value into a string?
>This is in the comp.lang.c FAQ; you need to introduce a level of
>indirection to force the macro to be evaluated before adding quotes:

Thanks to all who responded.  It is in the FAQ (11.17, under "ANSI
and ISO C").  I read the "Preprocessor" section (10) but obviously
didn't look far enough!  Mea culpa.

Hm ... looking at the recently posted CLC FAQ I see that there is
a link to 11.17 from the Preprocessor section (10.23).  Why didn't
I spot it?  Answer: the C FAQ that comes up in Google is Steve
Summit's hypertext version (http://www.eskimo.com/~scs/C-faq/top.html)
and it's out of date.  It is missing 10.23's link to 11.17.  I'll go
straight to the latest plaintext version from now on
(http://www.faqs.org/faqs/C-faq/faq/).

- David Librik
--



Mon, 29 Nov 2004 23:22:44 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Preprocessor havoc: Pasting values of macros rather than names

2. preprocessor macro for repeating?

3. C Macros textually substituting for preprocessor directives.

4. Non-recursive preprocessor macros?

5. Preprocessor / Macro substitution

6. C preprocessor - Macros

7. Deciphering a regexp preprocessor macros

8. Non-recursive preprocessor macros?

9. Preprocessor: defining a macro inside a #define directive

10. Question about preprocessor evaluation of expressions in macro expansions

11. Preprocessor macros

12. Use of ellipsis in preprocessor macros

 

 
Powered by phpBB® Forum Software