main()运行结果
main2()运行结果
#include<stdio.h>
#include<malloc.h>
//宏定义(宏替换):只是简单的替换,不会给你做任何的操作
#define EXP (5+2*3)
//求平方
#define SQUARE(number) ((number)*(number))
//输出错误的宏函数
#define clog(error) printf("[log %d]:%s\n",__LINE__,error)
//比较大小
#define MAX(a,b) ((a)>(b)?(a):(b))
//内存分配
#define MALLOC(type,size) malloc(sizeof(type)*size)
//#把后面的参数变成字符串
#define toString(value) #value
//## 把两个参数连接到一起
#define VAR(var) maye_##var
//#@ 把后面的参数变成字符
#define toChar(value) #@value
//是否产生错误
#define ERROR 0
#define MAYE //空宏
#if ERROR
//输出错误信息,终止编译
#error 发生了一个错误,请查证后再波
#else
//#include<easyx.h>
#endif
void def()
{
#if 0
printf("#if 1\n");
#elif 1
printf("#elif 1\n");
#else
printf("#else\n");
#endif // 1
//判断是否定义了某个宏
#ifdef MAYE //ifndef
printf("定义了MAYE\n");
#else
printf("未定义了MAYE\n");
#endif // DEBUG
#if !defined(MAYE)
printf("#if defined(MAYE)\n");
#else
printf("#else\n");
#endif
}
int main2()
{
def();
/*
//maye_age
int VAR(age) = 50;
char VAR(name)[10] = "maye";
printf("%d %s\n", maye_age,maye_name);
putchar(toChar(1234));
putchar(toChar(\n));
char* tel = toString(13145616991);
puts(tel);
int age = 562;
puts(toString(age)); //age
*/
return 0;
}
int main()
{
int* p = malloc(sizeof(int) * 5);
if (!p)
{
printf("memory allocated failed\n");
}
int* pn = MALLOC(int, 5);
double* pd = MALLOC(double, 6);
//取消宏定义
//#undef MALLOC
char* pc = MALLOC(char, 256);
printf("%d\n", EXP); //11
//2*5+2*3 => 2*(5+2*3)
printf("%d\n", 2 * EXP); //想要的结果就是:22 实际上结果是:16
printf("%d\n", SQUARE(5)); //25
//SQUARE(5+2) 5+2*5+2 = 17
printf("%d\n", SQUARE(5 + 2)); //想要的结果就是:49 实际的结果是:17
clog("内存申请失败");
clog("helo");
printf("%d\n", MAX(45, 6));
printf("%lf\n", MAX(3.14, 5.20));
printf("%c\n", MAX('A', 'a'));
return 0;
}