宏函数不是函数,比如:#define MAX 1,但是用起来象函数,如01。特点:不大,一定场景下效率比函数(比如例子02,很多对栈的处理)高
真正的函数要有返回值、参数类型、函数体
宏函数不做语法简单,就是替换,是简单的文本处理,一般不认为是C语言的内容
栈,先进后出,First In Last Out,一种容器
位置高的,地址高,所以认为是开口向下的。如下图
栈的单位名称是堆栈帧、活动记录
函数参数传递用到栈,所以没有栈就没有函数。比如:函数返回值地址(值大放栈,值小放寄存器,如02例子)、函数参数(压栈顺序,用完后函数还是主函数清理)、临时变量
01 宏函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MYADD(x,y) ((x)+(y))**
void main() {
int a = 1;
int b = 1;
printf("%d\n", MYADD(a, b));
system("pause");
}
02 压栈例子
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int myadd(int a, int b)
{
int t_a = a;
int t_b = b;
return a + b;
}
void test1()
{
int ret;
ret = myadd(1, 1);
int a = 2;
}
void main() {
test1();
system("pause");
}