On Sat, 16 Aug 1997, Daniel Koepke wrote:
>You'll have to do a bit of work to get this to work (probably, anyway), and
>it isn't the best it can be [if you figure out how to "stringify" things
>with GCC you might be able to get some extended information, but I'm
>uncertain how to do that]. But this is the basic code. Essentially it
11.17: I'm trying to use the ANSI "stringizing" preprocessing operator
`#' to insert the value of a symbolic constant into a message,
but it keeps stringizing the macro's name rather than its value.
A: You can use something like the following two-step procedure to
force a macro to be expanded as well as stringized:
#define Str(x) #x
#define Xstr(x) Str(x)
#define OP plus
char *opname = Xstr(OP);
This code sets opname to "plus" rather than "OP".
An equivalent circumlocution is necessary with the token-pasting
operator ## when the values (rather than the names) of two
macros are to be concatenated.
References: ANSI Sec. 3.8.3.2, Sec. 3.8.3.5 example; ISO
Sec. 6.8.3.2, Sec. 6.8.3.5.
and later:
11.18: What does the message "warning: macro replacement within a
string literal" mean?
A: Some pre-ANSI compilers/preprocessors interpreted macro
definitions like
#define TRACE(var, fmt) printf("TRACE: var = fmt\n", var)
such that invocations like
TRACE(i, %d);
were expanded as
printf("TRACE: i = %d\n", i);
In other words, macro parameters were expanded even inside
string literals and character constants.
Macro expansion is *not* defined in this way by K&R or by
Standard C. When you do want to turn macro arguments into
strings, you can use the new # preprocessing operator, along
with string literal concatenation (another new ANSI feature):
#define TRACE(var, fmt) \
printf("TRACE: " #var " = " #fmt "\n", var)
See also question 11.17 above.
References: H&S Sec. 3.3.8 p. 51.
Source: comp.lang.c FAQ
--
greerga@muohio.edu me@null.net | Genius may have its limitations, but stupidity
http://www.muohio.edu/~greerga | is not thus handicapped. -- Elbert Hubbard
+------------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
| http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
+------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 12/08/00 PST