assert 断言函数
头文件
#include <assert.h>
使用说明
- void assert(int expression);
- assert (表达式) 如果表达式为真,就继续执行下面的,如果为假就会中断程序执行
- 没有返回值
- 常用来 断言 空指针
例子:
//模拟实现strcpy
char * my_strcpy(char * destination, const char * source){//定义
int i = 0;
assert(destination != NULL || source != NULL);
while (source[i]!= '\0' && destination[i] != '\0' ){
destination[i] = source[i];
i++;
}
char * p = destination;
return p;
system() 函数
头文件
#include
使用说明
C 库函数 int system(const char *command) 把 command 指定的命令名称或程序名称传给要被命令处理器执行的主机环境,并在命令完成后返回。 如 system(“clear”) 终端清屏。
例子
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main ()
{
char command[50];
strcpy( command, "ls -l" );
system(command);
return(0);
}
列出Linux 文件详细信息
abs()绝对值函数
头文件
#inlclude <stdlib.h>
使用说明 :
是一个绝对值函数 abs(数学表达式),直接 使用。abs() 函数只适用于整数,如果需要计算浮点数的绝对值,需要使用 fabs() 函数。
例子:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
//输出菱形
{
int i , j , k ;
for(i= 1 ;i <= 7; i++) {
for(j = 1 ; j <=abs (4-i) ;j++){//空格与行数的关系
printf(" ");
}
for(k = 1 ; k <= 7- 2*abs(4-i) ;k++){//*号与行数的关系
printf("*");
}
printf("\n");//每行循环结束后,空行
}
return 0;
}
//输出结果
*
***
*****
*******
*****
***
*
四、指数函数 pow
头文件
#include <math.h>
使用说明
直接使用 double pow(double x ,double y)表示x的y次方,注意,是double类型的
例子
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int n ,a;
int i = 1;
int sum = 0;
int SUM = 0;
printf("please input n and a \n");
scanf("%d %d",&n ,&a);
for (;i <= n; i++){
sum = sum*10+a;
printf("%d + ",sum);
SUM += sum;
// sum = pow(10,i)*a +sum;//指数函数 10的i次方
// SUM += sum;
}
printf("\b");
printf("\b");
printf("\b");//光标回退 "\b"表示回退一格
printf(" = %d\n",SUM);
return 0;
}
在这个例子中:printf(“\b”)表示光标回退一格,在缓存区中,后面的输出元素会将前面的部分覆盖,利用好光标回退,可以省事的输出自己想要的输出结果。
// 目前是这两天遇到的一些函数,后续在继续跟新。