1.strlen()字符串长度。
#include
#include
using namespace std;
int main()
{
char a[101];
fgets(a, 101, stdin);
int c = strlen(a);
cout << c << endl;
return 0;
}
2.strcmp()字符串比较
strcmp(str1, str2)
该函数返回值如下:
如果返回值小于 0,则表示 str1 小于 str2。
如果返回值大于 0,则表示 str1 大于 str2。
如果返回值等于 0,则表示 str1 等于 str2。
#include
#include
using namespace std;
const int N = 15;
int main()
{
char str1[N] = "abcdef";
char str2[N] = "ABCDEF";
int ret;
ret = strcmp(str1, str2);
if(ret < 0)
{
printf("str1 小于 str2");
}
else if(ret > 0)
{
printf("str1 大于 str2");
}
else
{
printf("str1 等于 str2");
}
return 0;
}
3.strcpy()字符串拷贝
#include
#include
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
4.memset()暴力清空
#include
#include
#include
int main ()
{
char str[50];
strcpy(str,"This is string.h library function");
puts(str);
memset(str,0,strlen(str));
puts(str);
return(0);
}
5.memcpy()暴力拷贝
#include
#include
int main(void)
{
char src[] = "***";
char dest[] = "abcdefg";
printf("使用 memcpy 前: %s\n", dest);
memcpy(dest, src, strlen(src));
printf("使用 memcpy 后: %s\n", dest);
return 0;
}
1.pow()次方函数
#include
#include
int main ()
{
printf("值 8.0 ^ 3 = %lf\n", pow(8.0, 3));
printf("值 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
return(0);
}
2.sin()cos()等三角函数asin()反函数
#include
#include
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 45.0;
val = PI / 180;
ret = sin(x*val);
printf("%lf 的正弦是 %lf 度", x, ret);
return(0);
}
3.sqrt()平方根
#include
#include
int main ()
{
printf("%lf 的平方根是 %lf\n", 4.0, sqrt(4.0) );
printf("%lf 的平方根是 %lf\n", 5.0, sqrt(5.0) );
return(0);
}
4.fabs()绝对值(浮点数)
#include
#include
int main ()
{
int a, b;
a = 1234;
b = -344;
printf("%d 的绝对值是 %lf\n", a, fabs(a));
printf("%d 的绝对值是 %lf\n", b, fabs(b));
return(0);
}
5.double ceil(double x) 返回大于或等于 x 的最小的整数值
#include
#include
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
return(0);
}
#include
#include
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
printf("Value1 = %.1lf\n", floor(val1));
printf("Value2 = %.1lf\n", floor(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
return(0);
}
1.qsort()快排
#include
#include
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)b - *(int*)a );
}
int main()
{
int n;
printf("排序之前的列表:\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\n排序之后的列表:\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
return(0);
}
2.malloc()free()C语言动态内存分配
#include
#include
#include
int main()
{
char *str;
/* 最初的内存分配 */
str = (char *) malloc(15);
strcpy(str, "runoob");
printf("String = %s, Address = %u\n", str, str);
/* 重新分配内存 */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
free(str);
return(0);
}
3.rand()随机数
#include
#include
#include
int main()
{
int i, n;
time_t t;
n = 5;
srand((unsigned) time(&t));
for( i = 0 ; i < n ; i++ )
{
printf("%d\n", rand() % 50);
}
return(0);
}
1.clock()程序从启动到现在的毫秒数
#include
#include
int main()
{
clock_t start_t, end_t;
double total_t;
int i;
start_t = clock();
printf("程序启动,start_t = %ld\n", start_t);
printf("开始一个大循环,start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("大循环结束,end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("CPU 占用的总时间:%f\n", total_t );
printf("程序退出...\n");
return(0);
}
2.time()
#include
#include
int main ()
{
time_t seconds;
seconds = time(NULL);
printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);
return(0);
}
1.int isdigit(int c) 检查所传的字符是否是十进制数字字符
#include
#include
int main()
{
int var1 = 'h';
int var2 = '2';
if( isdigit(var1) )
{
printf("var1 = |%c| 是一个数字\n", var1 );
}
else
{
printf("var1 = |%c| 不是一个数字\n", var1 );
}
if( isdigit(var2) )
{
printf("var2 = |%c| 是一个数字\n", var2 );
}
else
{
printf("var2 = |%c| 不是一个数字\n", var2 );
}
return(0);
}
2.void isalpha(int c) 检查所传的字符是否是字母
#include
#include
int main()
{
int var1 = 'd';
int var2 = '2';
int var3 = '\t';
int var4 = ' ';
if( isalpha(var1) )
{
printf("var1 = |%c| 是一个字母\n", var1 );
}
else
{
printf("var1 = |%c| 不是一个字母\n", var1 );
}
if( isalpha(var2) )
{
printf("var2 = |%c| 是一个字母\n", var2 );
}
else
{
printf("var2 = |%c| 不是一个字母\n", var2 );
}
if( isalpha(var3) )
{
printf("var3 = |%c| 是一个字母\n", var3 );
}
else
{
printf("var3 = |%c| 不是一个字母\n", var3 );
}
if( isalpha(var4) )
{
printf("var4 = |%c| 是一个字母\n", var4 );
}
else
{
printf("var4 = |%c| 不是一个字母\n", var4 );
}
return(0);
}
3.int isupper(int c) 检查所传的字符是否是大写字母;int islower(int c) 检查所传的字符是否是小写字母。是的话返回非零值,否则返回0。