• #define的一个小技巧


    /* atof example: sine calculator */
    #include <stdio.h>      /* printf, fgets */
    #include <stdlib.h>     /* atof */
    #include <math.h>       /* sin */
    
    
    
    #define vfd_com_timeout_clear()     do{vfd_com_cnt = 0;}while(0)
    #define vfd_com_timeout_recount()   do{vfd_com_cnt = 0;}while(0)
    
    /*
    判断等待时间是否超时
    */
    int is_eload_com_timeout(void)
    {
        //电子负载超过2s时间尚未反馈
        if (1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    
    int main ()
    {
        double n, m;
        double pi = 3.1415926535;
        char buffer[256] = {"000000.01212
    234"};
    
        int vfd_com_cnt = 5;
        printf("vfd_com_cnt = %d
    ",vfd_com_cnt);
        vfd_com_timeout_clear();
        printf("vfd_com_cnt = %d
    ",vfd_com_cnt);
    
        if (!is_eload_com_timeout())
        {
    		printf("oooo
    ");
        }
    	else
    	{
    		printf("kkkkk
    ");
    	}
    	
    
        n = atof (buffer);
        printf("n=%f
    ", n);
        m = sin (n * pi / 180);
        printf ("The sine of %f degrees is %f
    ", n, m);
        return 0;
    }
    
    int is_eload_com_timeout(void)
    {
    
        if (1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    
    int main ()
    {
        double n, m;
        double pi = 3.1415926535;
        char buffer[256] = {"000000.01212
    234"};
    
        int vfd_com_cnt = 5;
        printf("vfd_com_cnt = %d
    ",vfd_com_cnt);
        do{vfd_com_cnt = 0;}while(0);
        printf("vfd_com_cnt = %d
    ",vfd_com_cnt);
    
        if (!is_eload_com_timeout())
        {
      printf("oooo
    ");
        }
     else
     {
      printf("kkkkk
    ");
     }
    
    
        n = atof (buffer);
        printf("n=%f
    ", n);
        m = sin (n * pi / 180);
        printf ("The sine of %f degrees is %f
    ", n, m);
        return 0;
    }
    

    上文已经就将源代码和预编译后的比分文字合作一起了。

    #define vfd_com_timeout_clear()     do{vfd_com_cnt = 0;}while(0)

    这个代码我起初以为会有问题,应为括号里面是空的,

    但是使用gcc -E .atofc.c -o atofc.i后,发现直接替换了,因此该方法可以使用。

    但是注意,宏定义不检查语法的,因此写宏定义的时候要格外小心。

  • 相关阅读:
    虚幻4游戏开发_3_创建与继承材质
    Python 之 读取txt文件
    Guava ---- Concurrent并发
    leetCode 67.Add Binary (二进制加法) 解题思路和方法
    poj 1331 Multiply
    二叉树的三叉存储
    FTPClientUtil FTPclient工具
    HDU1018 Big Number n!的位数
    MyBatis參数格式化异常解决方式:MyBatisSystemException:
    Spark:大数据的电花火石!
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007318.html
Copyright © 2020-2023  润新知