wap 网站 开发,蛋糕店网页设计图片,在线做漫画的网站好,wordpress获取当前网址目录
宏定义
宏函数
1.注释事项
2.注意事项
宏(Macro)用法
常量定义
简单函数实现
类型检查
条件编译
宏函数计算参数个数
宏定义进行类型转换
宏定义进行位操作
宏定义进行断言
总结 宏定义
#include stdio.h
#include string.h
#incl…目录
宏定义
宏函数
1.注释事项
2.注意事项
宏(Macro)用法
常量定义
简单函数实现
类型检查
条件编译
宏函数计算参数个数
宏定义进行类型转换
宏定义进行位操作
宏定义进行断言
总结 宏定义
#include stdio.h
#include string.h
#include stdlib.h#define MAX 1025 //定义宏int main()
{system(pause);return EXIT_SUCCESS;
} 宏函数
宏函数通常将一些比较频繁短小的函数封装为宏函数。减少一些入栈出栈的时间消耗。
在C语言中宏Macro是预处理器的一种功能它允许你定义一种简写形式来代替一段特定的代码。宏定义的基本形式如下
#include stdio.h
#include string.h
#include stdlib.h#define MYADD(x,y) x y //宏函数int main()
{int a 10;int b 20;printf(ab %d\n,MYADD(a,b));system(pause);return EXIT_SUCCESS;
}
运行结果 宏函数在预处理中做了一个替换 就是将 a 和 b替换成x和y。
宏函数是使用宏定义的函数风格的宏。它们可以像普通函数那样调用但最终会被预处理器替换成相应的代码减少入栈出栈的时间。 1.注释事项
宏函数要保证运算的完整性才能执行可以查看下面代码处理流程
#include stdio.h
#include string.h
#include stdlib.h#define MYADD(x,y) x y //宏函数int main()
{int a 10;int b 20;int result MYADD(a, b) * 10;printf(result %d\n, result);system(pause);return EXIT_SUCCESS;
}
打印结果 从上面结果来看10 20 * 10 结果是等于300才符合我们预期这个是运算完整性导致的。就是先乘除后加减问题
这个问题是可以解决的通过()来处理
#define MYADD(x,y) ((x) (y)) //宏函数2.注意事项
宏函数在一定的程度上会比普通的函数效率高
#include stdio.h
#include string.h
#include stdlib.h#define MYADD(x,y) ((x) (y)) //宏函数void myAdd(int x,int y) { //x会在栈上定义y也会 . 所以宏函数会有一定优势return x y;
}int main()
{int a 10;int b 20;int result MYADD(a, b) * 10;printf(result %d\n, result);system(pause);return EXIT_SUCCESS;
}
可以从宏函数和普通函数对比一个没有栈的开销一个有开销。
普通函数会有入栈和出栈时间上的开销 宏(Macro)用法
常量定义
#include stdio.h
#include string.h
#include stdlib.h#define MAX 1024int main()
{printf(MAX %d\n, MAX);system(pause);return EXIT_SUCCESS;
}
运行结果 有参数宏
#include stdio.h
#include string.h
#include stdlib.h#define MYADD(x,y) ((x) (y)) //宏函数int main()
{int a 10;int b 20;int result MYADD(a, b);printf(result %d\n, result);system(pause);return EXIT_SUCCESS;
}
运行结果 宏运算链接符
#include stdio.h
#include string.h
#include stdlib.h#define MAX 100
#define ROW 100
#define GET_MAX(x,y) \
x xy;\
y xy;int main()
{int a 10;int b 10;GET_MAX(a, b)printf(random %d %d\n\n, a,b);system(pause);return EXIT_SUCCESS;
}
从上面代码来看\表示链接符号运行完第一个就执行第二个 无参数宏
#include stdio.h
#include string.h
#include stdlib.h#define MAX 100
#define ROW 100
#define RANDOM (-1.0 2.0*(double)rand() / RAND_MAX)int main()
{int v 10;printf(random %lf\n\n,RANDOM);system(pause);return EXIT_SUCCESS;
} 类型检查
#include stdio.h
#include string.h
#include stdlib.h#define CHECK_TYPE(a) generic((a), \int: printf(int), \char: printf(char), \float: printf(float), \default: printf(other type) \
)int main()
{int v 10;//CHECK_TYPE(v);printf(%s, _Generic(v));system(pause);return EXIT_SUCCESS;
} 条件编译
#include stdio.h
#include string.h
#include stdlib.h#define DEBUG#ifdef DEBUG
#define PRINT_DEBUG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define PRINT_DEBUG(fmt, ...)
#endifint main()
{int v 10;char value[] 达帮主;PRINT_DEBUG(%s\n,value);system(pause);return EXIT_SUCCESS;
}
运行结果 如果删掉define就不会有打印 宏函数计算参数个数
#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define VA_SIZE(...) \GET_MACRO(__VA_ARGS__, 4, 3, 2, 1, 0)#define SHOW_PARAM_COUNT(...) \printf(Number of parameters: %d\n, VA_SIZE(__VA_ARGS__))// 使用
SHOW_PARAM_COUNT(1, 2); // 输出Number of parameters: 2 宏定义进行类型转换
#define CONTAINER_OF(ptr, type, member) \((type *)((char *)(ptr) - (char *) ((type *)0)-member)) 宏定义进行位操作 #define SET_BIT(x, bit) ((x) | (1 (bit)))
#define CLEAR_BIT(x, bit) ((x) ~(1 (bit)))
#define FLIP_BIT(x, bit) ((x) ^ (1 (bit)))
#define GET_BIT(x, bit) (((x) (bit)) 1) 宏定义进行断言
#define ASSERT(expr) \if (!(expr)) { \printf(Assertion failed: %s\n, #expr); \exit(1); \} 总结
1.宏函数要保证运算的完整性。
2.宏函数在一定程度上会比普通函数效率高普通函数会有入栈和出栈时间上的开销。
3.通常会吧调用频繁的短小的函数封装为宏函数。
4.宏函数的优点以空间换时间。 文章转载自: http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.dpruuode.cn.gov.cn.dpruuode.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.nggry.cn.gov.cn.nggry.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn