How to print a pound / hash via C preprocessor?

前端 未结 5 1689
失恋的感觉
失恋的感觉 2021-01-12 00:20

I need help doing the following:

a preprocessor macro label(x) shall output \"#x\", e.g.,

#define label(x) ...

if I call label(anam

5条回答
  •  無奈伤痛
    2021-01-12 00:59

    I don't think you can, which is not wholly unreasonable since the output of the C preprocessor should not produce an unquoted '#' because that would indicate a pre-processor directive, and you cannot generate pre-processor directives on the fly like that.

    In other words, the C preprocessor is a preprocessor for C (and C++) and not a completely general purpose tool.

    Either use an alternative macro processor (m4 is the standard recommendation on Unix-like systems), or go about things differently.

    For example, have the macro replacement:

    #define label(x)    !@!x
    

    Then post-process the output replacing '!@!' with '#'.

    (The imake program uses a similar stunt; the C preprocessor does most of the work, but its output doesn't preserve line breaks needed by 'make', so 'imake' uses the notation '@@\' or thereabouts to indicate where line breaks need to be inserted after the C preprocessor has done its worst.)

提交回复
热议问题