1,精度转化
(1)强制类型转化 例:float pi=3.14; a=(int)pi;此时a=3;
(2)隐式转化 1)低精度向高精度转化,安全无错误;2)小范围向大范围转化(指最大数据比如unsigned int>int)
2,math.h数据库编译链接时需要 -lm
3,带参宏和普通宏
(1)宏定义在编译时直接替换,没有调用开销
普通宏:#define U32 int //用U32直接替代int
例如
#define s(x) x+x
如果调用
int x=3,a=0;
a=s(x)*s(x);
此时a的值等于x+x*x+x
所以a的值为15而不是36
(2)函数会检查你的参数和返回类型而宏定义没有类型也就谈不上检查
(3)如果函数体短小,需要用函数检查参数和返回值的功能,要提高效率可以用内联函数
关键字inline
例如
inline int max(int x,int y){}
如果使用的话编译时直接替代原来使用函数的位置
4,函数的递归以及栈的概念及特点
(1)栈是限定仅在表头进行插入和删除操作的线性表。
图中可看出进出栈时都要从上面出去从上面进去。
例题:
对于A选项a进a出b进b出c进c出d进d出
对于B选项ab进b出c进c出d进d出a出
对于C选项abc进c出d进d出b出a出
(2)函数递归调用
例子:
//计算阶乘(递归)
int jiecheng(int a)
{
if (a <= 0)
{
printf("参数错误.\n");
return -1;
}
//回归条件
if (a == 1)
{
return 1;
}
return a * jiecheng(a-1);
}
//递归解决求fibo数列某一项的值: 1 1 2 3 5 8 13 21 34 55
int fibo(int num)
{
if (num > 0)
{
if (1 == num)
{
return 1;
}
if (2 == num)
{
return 1;
}
return fibo(num-1) + fibo(num-2);
}
}
5,自己实现字符串的比较my_strcmp()
int my_strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0 && *s2 == 0)
break;
s1++;
s2++;
}
if (*s1 != *s2)
{
return (*s1 > *s2 ? 1 : -1);
}
if (*s1 == *s2)
return 0;
}
6,自己实现复制字符串char *my_strcpy(char *des, const char *src)
{
char *p_store = des;
while ((*des++=*src++) != 0);
return p_store;
}
7,main()函数的参数
main函数作为整个程序的入口,返回值给了他的父进程
char *argv[]指针数组:[]优先级很高,argv[]代表数组,里面所有的元素是char*类型,每个main函数的参数都是字符串(char *)
argc代表main函数参数个数