———–Java培训、Android培训、IOS培训、.Net培训、期待与您交流!————
本节是个人学习过程中的笔记,供初学者一起学习,欢迎大家批评指正,留言参与讨论,谢谢。
本节涉及到预处理三种类型的实现,涉及很多细节,详细变化请参考代码,谢谢。
macro.c文件如下:
#include <stdio.h>
#include "division.h"
#define TEST 45 //此处不能有分号
//#define EXIST
#define MULTI(a,b) ((a)*(b))
int main()
{
void testifendif();
testifendif();
int a = 18, b = 6;
printf("a * b = %d\n",MULTI(a,b));
printf("%d\n\n",MULTI(2+3,5+3));
printf("a / b = %d\n",division(a,b));
//指向函数的指针
int(*p)(int,int) = division; //必须括起来,(*p)
printf("a / b = %d\n",p(a,b));
printf("a / b = %d\n",(*p)(a,b));
}
void testifendif()
{
printf("the const TEST = %d\n",TEST);
#if TEST>=50
printf("the const TEST >= 50\n");
#elseif TEST>=30
printf("the const TEST < 50 && TEST > 30\n");
#else
printf("the const TEST < 30\n");
#endif
//#ifdef EXIST
// printf("EXIST\n");
//#endif
//
//#ifndef EXIST
// printf("NO EXIST\n");
//#endif
#ifdef EXIST
printf("EXIST\n");
#else
printf("NO EXIST, let's create EXIST !\n");
#define EXIST 100
#endif
printf("the const EXIST= %d\n",EXIST);
}
division.h 文件如下:
#ifndef DIVISION_H
#define DIVISION_H
int division(int,int);
#endif
division.c 文件如下:
int division(int a,int b)
{
return a/b;
}
运行结果如下: