Accessing enumeration strings in macros

K

kenkahn

Given the following simple c program

#include <stdio.h>

enum ATTRTYPE {
CFG_ATTR = 3000
};

int main() {
#define xx(id) printf("ATTR=%s\n",#id);
xx(CFG_BADATTR);
return 0;
}

Compiling this on AIX, Solaris or Linux using gcc (or xlC or CC)
results in the following output:

ATTR=CFG_ATTR

Looking at the preprocessor shows the macro expanding to

printf("ATTR=%s\n","CFG_BADATTR");

Is this use of '#' in a macro to access the literal string of an
enumeration value documented? I can't find it in the gcc manual (or
any other C compiler manual). But it does seem to work on all
platforms for multiple C compilers. Can I assume it will continue to
work or is this a fluke?

(I inherited this code and just recently figured out what this macro
was doing)
 
B

Ben Pfaff

kenkahn said:
Is this use of '#' in a macro to access the literal string of an
enumeration value documented? I can't find it in the gcc manual (or
any other C compiler manual). But it does seem to work on all
platforms for multiple C compilers. Can I assume it will continue to
work or is this a fluke?

The # macro operator is part of the C standard, which defines it
like this:

6.10.3.2 The # operator

Constraints
1 Each # preprocessing token in the replacement list for a
function-like macro shall be followed by a parameter as the
next preprocessing token in the replacement list.

Semantics
2 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. Each occurrence of white
space between the argument's preprocessing tokens becomes a
single space character in the character string
literal. White space before the first preprocessing token
and after the last preprocessing token composing the
argument is deleted. Otherwise, the original spelling of
each preprocessing token in the argument is retained in the
character string literal, except for special handling for
producing the spelling of string literals and character
constants: a \ character is inserted before each " and \
character of a character constant or string literal
(including the delimiting " characters), except that it is
implementation-defined whether a \ character is inserted
before the \ character beginning a universal character
name. If the replacement that results is not a valid
character string literal, the behavior is undefined. The
character string literal corresponding to an empty argument
is "". The order of evaluation of # and ## operators is
unspecified.
 
E

Eric Sosman

kenkahn wrote On 03/28/07 14:30,:
Given the following simple c program

#include <stdio.h>

enum ATTRTYPE {
CFG_ATTR = 3000
};

int main() {
#define xx(id) printf("ATTR=%s\n",#id);
xx(CFG_BADATTR);
return 0;
}

Compiling this on AIX, Solaris or Linux using gcc (or xlC or CC)
results in the following output:

ATTR=CFG_ATTR
[...]

Two possibilities: (1) All those compilers are broken
in exactly the same bizarre way, or (2) You're not telling
the truth.

I reject (1) as not credible, viewing (2) as the likely
explanation. Bad kenkahn! BAD, BAD, BAD kenkahn!

(In other words, report actual code and actual output
and actual error messages, not approximations. Thank you.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top