What does #x inside a C macro mean?

前端 未结 4 1823
死守一世寂寞
死守一世寂寞 2020-11-27 03:03

For example I have a macro:

#define PRINT(int) printf(#int \"%d\\n\",int)

I kinda know what is the result. But how come #int repersent the

4条回答
  •  Happy的楠姐
    2020-11-27 03:45

    In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro.

    In this case, if you call PRINT(5) the macro expansion will be printf("5" "%d\n", 5); which will print 5 5; not very useful; however if you call PRINT(5+5) the macro expansion will be printf("5+5" "%d\n", 5+5); which will print 5+5 10, a little less trivial.

    This very example is explained in this tutorial on the C preprocessor (which, incidentally, is the first Google hit for c macro pound sign).

提交回复
热议问题