• c: macro define 复杂的宏定义


     

     

    c: macro define 复杂的宏定义

     

     

     

     

    一、源码

     

    [root@rockylinux tmp]# cat macro_define.c 
    /**
     * 宏命定义的注意事项:
     *      1、带有参数的宏,参数使用时需要写在"()"之中,这样在宏展开时不会改变设计时的运算级别,保证结果正确;
     *              举例:#define MAX_INT(x,y) (x)>(y)?(x):(y)
     *      2、多行合并为一行(代码换行):当“宏定义”内容超过一行时,在语句末尾用"\"换行;在“空行的末尾”也要添加"\";
     *
     *      3、多行代码,可以使用"{}"和"()"。 
     *
     *
     */
    
    #include<stdio.h>
    #include<stdlib.h>
    
    
    #define MAX_USER() 20
    
    
    #define SAY(x,y) \
                    {\
                            printf("SAY:\t%s\n", (x)); \
                            printf("SAY:\t%s\n", (y)); \
                    }
    
    
    #define BOOK(x,y) {\
            printf("book_name=%s, \tbook_price=%s\n", (x), (y));\
            }
    
    
    #define LOOP(x) {       \
                            if((x)<=1){ exit(1);}\
                            int max = (x);\
                            for( int i=0; i<max; i++ ) printf("LOOP:\t\tcount=%d\n",i);\
                    }
    
    
    void msg()
    {
            for(int i=0; i<MAX_USER(); i= i + 2)
            printf("%d\t\tmax_user=%d,\tcurrent=%d\n", i/2, MAX_USER(), i);
    }
    
    
    int main(int argc, char *argv[])
    {
            LOOP(10);
            BOOK("Gangtie shi zenyang lianchengde", "23.5");
            SAY("hello world!", "hello houdini19.");
            msg();
    
            return 0;
    }
    [root@rockylinux tmp]# 
    [root@rockylinux tmp]# 
    [root@rockylinux tmp]# ./macro_define 
    LOOP:           count=0
    LOOP:           count=1
    LOOP:           count=2
    LOOP:           count=3
    LOOP:           count=4
    LOOP:           count=5
    LOOP:           count=6
    LOOP:           count=7
    LOOP:           count=8
    LOOP:           count=9
    book_name=Gangtie shi zenyang lianchengde,      book_price=23.5
    SAY:    hello world!
    SAY:    hello houdini19.
    0               max_user=20,    current=0
    1               max_user=20,    current=2
    2               max_user=20,    current=4
    3               max_user=20,    current=6
    4               max_user=20,    current=8
    5               max_user=20,    current=10
    6               max_user=20,    current=12
    7               max_user=20,    current=14
    8               max_user=20,    current=16
    9               max_user=20,    current=18
    [root@rockylinux tmp]# 
    [root@rockylinux tmp]# 
    

      

     

  • 相关阅读:
    [置顶] Extjs4 异步刷新书的情况下 保持树的展开状态
    控制系统中常用的名词术语
    开环控制系统与闭环控制系统
    反馈控制
    Tensorflow把label转化成one_hot格式
    Tensorflow——tf.nn.max_pool池化操作
    Tensorflow——tf.nn.conv2d卷积操作
    Tensoflow简单神经网络实现非线性拟合
    Tensorflow 中的 placeholder
    Tensorflow 中的constant、Session、placeholde和 Variable简介
  • 原文地址:https://www.cnblogs.com/lnlidawei/p/16439539.html
Copyright © 2020-2023  润新知