当前位置:网站首页>C语言进阶第46式:函数与宏分析
C语言进阶第46式:函数与宏分析
2022-04-21 18:11:00 【东川君】
函数与宏

宏是由预处理器直接替换展开的,编译器不知道宏的存在;
函数是由编译器直接编译的实体,调用行为由编译器决定;
多次使用宏会导致最终可执行程序的体积增大;
函数是跳转执行的,内存中只有一份函数体存在;
宏的效率比函数要高,因为是直接展开,无调用开销;
函数调用时会创建活动记录,效率不如宏;
函数与宏,示例如下
#include <stdio.h>
#define RESET(p, len) \
while( len > 0 ) \
((char*)p)[--len] = 0
void reset(void* p, int len)
{
while( len > 0 )
((char*)p)[--len] = 0;
}
int main()
{
int array[] = {1, 2, 3, 4, 5};
int len = sizeof(array);
int i = 0;
for(i=0; i<5; i++)
{
printf("array[%d] = %d\n", i, array[i]);
}
return 0;
}
宏的效率比函数稍高,但是其副作用巨大;
宏是文本替换,参数无法进行类型检查;
可以用函数完成的功能绝对不用宏;
宏的定义中不能出现递归定义;
宏的副作用,示例如下
#include <stdio.h>
#define _ADD_(a, b) a + b
#define _MUL_(a, b) a * b
#define _MIN_(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int i = 1;
int j = 10;
printf("%d\n", _MUL_(_ADD_(1, 2), _ADD_(3, 4)));
printf("%d\n", _MIN_(i++, j));
return 0;
}
宏的妙用
用于生成一些常规性的代码;
封装函数,加上类型信息;
宏的妙用,示例如下
#include <stdio.h>
#include <malloc.h>
#define MALLOC(type, x) (type*)malloc(sizeof(type)*x)
#define FREE(p) (free(p), p=NULL)
#define LOG_INT(i) printf("%s = %d\n", #i, i)
#define LOG_CHAR(c) printf("%s = %c\n", #c, c)
#define LOG_FLOAT(f) printf("%s = %f\n", #f, f)
#define LOG_POINTER(p) printf("%s = %p\n", #p, p)
#define LOG_STRING(s) printf("%s = %s\n", #s, s)
#define FOREACH(i, n) while(1) { int i = 0, l = n; for(i=0; i < l; i++)
#define BEGIN {
#define END } break; }
int main()
{
int* pi = MALLOC(int, 5);
char* str = "D.T.Software";
LOG_STRING(str);
LOG_POINTER(pi);
FOREACH(k, 5)
BEGIN
pi[k] = k + 1;
END
FOREACH(n, 5)
BEGIN
int value = pi[n];
LOG_INT(value);
END
FREE(pi);
LOG_POINTER(pi);
return 0;
}
小结
宏和函数并不是竞争对手;
宏能够接受任何类型的参数,效率高,易出错;
函数的参数必须是固定类型,效率稍低,不易出错;
宏可以实现函数不能实现的功能;
版权声明
本文为[东川君]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Bruce_Qee/article/details/124323402
边栏推荐
- TCP/IP协议
- Headline we media operation secret script. If you stick to it, you can beat 90% of the people
- 包装类
- Interface test framework practice (II) | interface request assertion
- LogStash~LogStash的input(input)
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- 移植openharmony之添加wifi驱动
- WPF学习笔记——概述
- 还剩下载次数,可留链接+邮箱
- 中介者模式
猜你喜欢

靶机渗透练习80-Momentum:1

Eating this open source gadget makes MCU development as efficient as Arduino

移动平台WorkPlus集成化办公,打造企业全场景业务生态

你必须懂也可以懂的微服务系列三:服务调用

【Redis】 使用Redis优化省份展示数据不显示

Download times remaining, link + email can be left

Shallow comparison between oceanbase and tidb - implementation plan

终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你

Linux~libc. so. 6 (glibc_2.28) (64bit) is redis-5.0.3-1 nfs. x86_ 64 required

刚拿的字节跳动offer“打水漂”
随机推荐
String(一个特殊的数据类型)
Variable与Tensor合并后,关于训练、验证的相关变化
[intensive reading of Thesis] perception based seam cutting for image stitching
MySQL - remote connection to non local MySQL database server, Error 1130: host 192.168.3.100 is not allowed to connect to this MySQL s
STM32 MCU memory manager code can be directly used in engineering
Build a short video platform and copy a demo of the login interface
The conflict between Russia and Ukraine raised concerns. The five eye network security department suggested that allies strengthen the protection of key infrastructure
Detailed explanation of kubernetes (III) -- kubernetes cluster components
Do we media sidelines really earn tens of thousands a month? This article is shared without privacy
单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗
Logstash ~ execution model of logstash
Linux~libc.so.6(GLIBC_2.28)(64bit) 被 redis-5.0.3-1.nfs.x86_64 需要
爬虫案例01
靶机渗透练习78-Thoth Tech
Interface test framework practice (I) | requests and interface request construction
MySQL的默认用户名和密码的什么?
What happens when the user sends a request to execute the controller method
"Industrial Internet plus safety production" to enhance the safety level of industrial enterprises
搭建JMeter+Jenkins+Ant持续化
Eating this open source gadget makes MCU development as efficient as Arduino