文档介绍:嵌入式系统程序设计
C/C++语言摘要
C语言的历史和特点
预处理程序
位运算
函数指针
C程序的移植
★
★
一、C语言的历史和特点
C语言的历史
C语言的特点
C语言的优良特性
结 T(m,n) m*n
………..
area=T(3,2);
宏展开: area=3*2;
不能加空格
带参数宏定义
一般形式: #define 宏名(参数表) 宏体
宏展开:形参用实参换,其它字符保留
宏体及各形参外一般应加括号()
例 #define T(m,n) m*n
………..
area=T(3,2);
宏展开: area=3*2;
例 #define CUBE(x) x*x*x
a=4; b=6;
z=CUBE(a+b);
宏展开:z=a+b*a+b*a+b;
一般写成: #define CUBE(x) ((x)*(x)*(x))
宏展开: z=((a+b)*(a+b)*(a+b));
#define FUNC(x,y) (x)>(y)?(x):(y)
…….
main()
{ int a,b,c,d,t;
…….
t=FUNC(a+b,c+d);
……
}
宏展开:t=(a+b)>(c+d)?(a+b):(c+d);
int func(int x,int y)
{ return(x>y?x:y);
}
main()
{ int a,b,c,d,t;
…….
t=func(a+b,c+d);
………
}
例 用宏定义和函数实现同样的功能
带参的宏与函数区别
带参宏
函数
处理过程
不分配内存
简单的字符置换
分配内存
先求实参值,再代入形参
处理时间
编译时
程序运行时
参数类型
无类型问题
定义实参,形参类型
程序长度
变长
不变
运行速度
不占运行时间
调用和返回占时间
Embest开发环境中的宏定义
……
#define rBWSCON (*(volatile unsigned *)0x1c80000)
#define rBANKCON0 (*(volatile unsigned *)0x1c80004)
#define rBANKCON1 (*(volatile unsigned *)0x1c80008)
#define rBANKCON2 (*(volatile unsigned *)0x1c8000c)
#define rBANKCON3 (*(volatile unsigned *)0x1c80010)
#define rBANKCON4 (*(volatile unsigned *)0x1c80014)
#define rBANKCON5 (*(volatile unsigned *)0x1c80018)
……
预定义宏
C89规范了五个固有的预定义宏,分别为:
__LINE__:行号
__FILE__:文件名
__DATE__:日期
__TIME__:时间
__STDC__:1-标准C编译器
0-非标准C编译器
预定义宏
#include <>
int main()
{
printf("The current file is: %s\n", __FILE__);
printf("The current line number is: %d\n", __LINE__);
printf("today is: %s\n", __DATE__);
printf("the time is: %s\n", __TIME__);
if(__STDC__ == 1)
printf("this is a standard compiler\n");
else if(__STDC__ == 0)
printf("this is not a standard compiler\n");
}
~/exp/pre/
功能:对源程序中的一部分内容只有满足某种条件的情况下才进行编译。
2. 条件编译
★
★
#ifdef 标识符
程序段1
#else
程序段2
#endif
形式1:
当标识符已经被定义过(使用#defin