根据参考测试
.h
#include <stdio.h> #define PrintNumber1(num) \ int iIntPart; \ double dDecimalPart; \ iIntPart = (int)num; \ dDecimalPart = num - iIntPart; \ printf("Integer part: %d\n", iIntPart); \ printf("Decimal part: %lf\n", dDecimalPart); \ #define PrintNumber2(num) { \ int iIntPart; \ double dDecimalPart; \ iIntPart = (int)num; \ dDecimalPart = num - iIntPart; \ printf("Integer part: %d\n", iIntPart); \ printf("Decimal part: %lf\n", dDecimalPart); \ } #define PrintNumber3(num) do { \ int iIntPart; \ double dDecimalPart; \ iIntPart = (int)num; \ dDecimalPart = num - iIntPart; \ printf("Integer part: %d\n", iIntPart); \ printf("Decimal part: %lf\n", dDecimalPart); \ }while(0) int test01();
讯享网
.c
讯享网#include "PrintNumber.h" static void test01_01(){ PrintNumber1(12.89); } static void test01_02(){ float fValue = 32.12; if (fValue > 0) PrintNumber2(fValue); } static void test01_03(){ float fValue = 32.12; if (fValue > 0) PrintNumber3(fValue); else printf("para err\n"); } int test01(){ test01_01(); test01_02(); test01_03(); return 0; }
总结起来就是:这样定义的目的是:使用do-while句式,可以吸掉一个分号,让我们可以像使用函数一样,来使用宏。
参考:
c语言宏定义_为什么C语言中复杂的函数式宏定义常用do while(0)的方式定义?_weixin_的博客-CSDN博客
C语言 if语句 大括号和不加大括号的区别_一个噗噗的博客-CSDN博客_c语言不加大括号

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/48350.html