How to print a pound / hash via C preprocessor?

前端 未结 5 1708
失恋的感觉
失恋的感觉 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:55

    You can do something like this:

    #define f(x) x
    #define label(a) f(#)a
    

    I tested this by running it directly through cpp (the C preprocessor) instead of through gcc. Example:

    cpp test > test.html
    

    Using the cpp that is part of gcc version 4.0.1.

    The only problem I noticed is that I get some extra unwanted output, namely the first 4 lines of the file are as follows:

    # 1 "test"
    # 1 ""
    # 1 ""
    # 1 "test"
    

提交回复
热议问题