zoukankan      html  css  js  c++  java
  • 参考http://www.cnblogs.com/openix/admin/EditPosts.aspx?opt=1

    宏:

    #define OW “I lo\

    ve you”

    输出:I love you

     #define OW “I lo\

        ve you”

    输出:I lo  ve you

     #define OW “I lo”\

                      “ve you”

    输出:I love you

    利用宏参数创建字符串:#运算符

    #define PSQR(X) printf(“The square of X is %d\n”,((X)*(X))

    则:

    PSQR(8)输出:The square of X is 64

    因此我们这里的双引号中的X被当成是个字符

    而#符号用作一个预处理运算符,它可以将语言符号转化为字符串,例如X是一个宏参量,那么#X可以把参数名转化为相应的字符串。例如

    #define PSQR(x) printf(“The square of “ #x “is %d\n”,((X)*(X))

    则:

    PSQR(8) 输出:The square of 8 is 64

    或者,如我我们定义int y=8;

    则:PSQR(y) 输出:The square of y is 64

    cpp的粘合剂:##运算符

    例如:

    #define cmd(name)\

    cmd_tbl cmd_##name

    故有:

    cmd(bootm);

    为:cmd_tbl cmd_bootm;

    可变宏:…和__VA_ARGS__    执行C99标准

    实际思想就是宏定义中参数列表的最后一个参数为三个句号。这样,预定义宏__VA_ARGS__就可以被用在替换部分中,以表明省略号代表什么。

    例如:

    #define PR(…) printf(__VA_ARGS__)

    则PR(“Howdy”);展开为:printf(“Howdy”);

       PR(“weight = %d, shipping =%.2f\n”,wt,sp);展开为:printf(“weight =%d,shipping =%.2f\n”,wt,sp);

    //程序variadic.c

    #include<stdio.h>

    #incude<math.h>

    #define PR(X,…) printf(“Message “ #x”: “ __VA_ARGS__)

    int main(void)

    {
             double x=48;

    double y;

    y=sqrt(x);

    PR(1,”x= %g\n”,x);

    PR(2,”x =%.2f, y=%.4f\n”,x,y);

    return 0;

    }

     

    第一个宏展开为printf(”Message “ “1” “: “ “x=%g\n”,x);

    第二个宏展开为printf (“Message “ “2” “: “ “x=%.2f, y=%.4f\n”,x,y);

    关于参数个数可变宏中最后一个逗号的处理:

    #define debug(fmt, arg...)  printf(fmt, ##arg)

    该处对可变参数的名字自己做了定义,且定义位arg,不再使用__VA_ARGS__,当我们调用debug时,至少有一个参数,也是对的:##arg意味着可以没有可变参数个数,并且当没有可变参数时由cpp去掉其前的逗号(GCC支持)

    #define debug(fmt, arg, ...) printf(fmt, arg, ##__VA_ARGS__)

    该处没有对可变参数定义自己的名字,必须使用__VA_ARGS__,当我们调用debug时,至少有两个参数:##__VA_ARGS__意味着可以没有可变参数个数,并且当没有可变参数时由cpp去掉其前的逗号(GCC支持)

    注意,对于上述两种情形,一定不能写成如下两种形式:

    #define debug(arg...)  printf(##arg)

    #define debug (...) printf(##__VA_ARGS__)

    这两种写法时错的,因为##意味着前边必定有逗号,所以错了

    但是下列写法时对的:

    #define debug(arg...)  printf(arg)

    #define debug(...) printf(__VA_ARGS__)

    当然,这时要是使用的话就必须至少有一个参数

  • 相关阅读:
    【转】win8.1下安装ubuntu
    Codeforces 1025G Company Acquisitions (概率期望)
    Codeforces 997D Cycles in Product (点分治、DP计数)
    Codeforces 997E Good Subsegments (线段树)
    Codeforces 1188E Problem from Red Panda (计数)
    Codeforces 1284E New Year and Castle Building (计算几何)
    Codeforces 1322D Reality Show (DP)
    AtCoder AGC043C Giant Graph (图论、SG函数、FWT)
    Codeforces 1305F Kuroni and the Punishment (随机化)
    AtCoder AGC022E Median Replace (字符串、自动机、贪心、计数)
  • 原文地址:https://www.cnblogs.com/openix/p/2367557.html
Copyright © 2011-2022 走看看