usage of # in the following statement

O

ooze

I have read the Programing language C

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 unspecified 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.


but I still could not figure out the usage of # in the FAQ. could
anyone help me? thanks in advance

in the FAQ , there is an example i can understand either
#define Str(x) #x
#define Xstr(x) Str(x)
#define OP plus
char *opname = Xstr(OP);
to produce the result opname="plus"

why can not i do it like the below:
#define str(x) #x
#define op plus
char * opname=str(op);
??
 
E

Eric Amick

I have read the Programing language C

6.10.3.2 The # operator

[definition snipped]
why can not i do it like the below:
#define str(x) #x
#define op plus
char * opname=str(op);
??

Because of this passage in 6.10.3.1:

After the arguments for the invocation of a function-like macro have
been identified, argument substitution takes place. A parameter in the
replacement list, unless preceded by a # or ## preprocessing token or
followed by a ## preprocessing token (see below), is replaced by the
corresponding argument after all macros contained therein have been
expanded.
 
O

ooze

Eric Amick said:
I have read the Programing language C

6.10.3.2 The # operator

[definition snipped]
why can not i do it like the below:
#define str(x) #x
#define op plus
char * opname=str(op);
??

Because of this passage in 6.10.3.1:

After the arguments for the invocation of a function-like macro have
been identified, argument substitution takes place. A parameter in the
replacement list, unless preceded by a # or ## preprocessing token or
followed by a ## preprocessing token (see below), is replaced by the
corresponding argument after all macros contained therein have been
expanded.

thanks, I see where I just lost
 
O

ooze

Eric Amick said:
I have read the Programing language C

6.10.3.2 The # operator

[definition snipped]
why can not i do it like the below:
#define str(x) #x
#define op plus
char * opname=str(op);
??

Because of this passage in 6.10.3.1:

After the arguments for the invocation of a function-like macro have
been identified, argument substitution takes place. A parameter in the
replacement list, unless preceded by a # or ## preprocessing token or
followed by a ## preprocessing token (see below), is replaced by the
corresponding argument after all macros contained therein have been
expanded.
thanks, I see where I lost
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top