- c语言学习计划
- C语言简介
- C语言基本语法
- C语言-控制结构
- C语言-输入输出
- C语言-数组和字符串
- C语言函数详解
- C语言指针详解
- 结构体和联合体
- C语言高级特性和内存管理
- c语言文件操作
- C 语言标准库
- C 语言标准库(二)
- C 语言标准库(三)
- C 语言标准库(四)
- C 语言标准库(五)
C 语言标准库
class math.h,stdlib.h,time.h,string.h,stdio.hC 语言标准库提供了一系列功能强大且常用的库函数,极大地扩展了 C 语言的功能。以下将详细介绍几个常用的标准库及其函数,包括 <math.h>、<stdlib.h>、<time.h> 等,涵盖数学运算、通用工具函数、时间处理等方面。
1. <math.h> —— 数学库
<math.h> 提供了许多数学函数,包括基本的三角函数、指数函数、对数函数等。常用函数如下:
1.1 三角函数
double sin(double x):返回x的正弦值,x以弧度为单位。double cos(double x):返回x的余弦值。double tan(double x):返回x的正切值。double asin(double x):返回x的反正弦值,结果为弧度。double acos(double x):返回x的反余弦值。double atan(double x):返回x的反正切值。
示例:计算角度的正弦值
#include <stdio.h>
#include <math.h>
int main() {
double angle = 0.5; // 弧度值
double sine_value = sin(angle);
printf("sin(%.2f) = %.2f\n", angle, sine_value);
return 0;
}
1.2 幂运算和对数函数
double pow(double base, double exp):计算base的exp次幂。double sqrt(double x):返回x的平方根。double exp(double x):计算e的x次幂。double log(double x):返回x的自然对数。double log10(double x):返回x的以 10 为底的对数。
示例:计算幂和平方根
#include <stdio.h>
#include <math.h>
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
printf("%.2f^%.2f = %.2f\n", base, exponent, result);
double num = 16.0;
printf("sqrt(%.2f) = %.2f\n", num, sqrt(num));
return 0;
}
1.3 舍入函数
double ceil(double x):返回大于等于x的最小整数。double floor(double x):返回小于等于x的最大整数。double round(double x):返回最接近x的整数。
示例:舍入操作
#include <stdio.h>
#include <math.h>
int main() {
double num = 5.67;
printf("ceil(%.2f) = %.2f\n", num, ceil(num));
printf("floor(%.2f) = %.2f\n", num, floor(num));
printf("round(%.2f) = %.2f\n", num, round(num));
return 0;
}
2. <stdlib.h> —— 通用工具库
<stdlib.h> 提供了通用的工具函数,包括动态内存管理、随机数生成、排序、转换等功能。
2.1 动态内存管理
void *malloc(size_t size):分配size字节的内存,返回指向分配内存的指针。void *calloc(size_t nmemb, size_t size):分配nmemb * size字节的内存,并初始化为 0。void *realloc(void *ptr, size_t size):重新分配内存,将ptr指向的内存扩展到size字节。void free(void *ptr):释放ptr指向的内存。
示例:动态分配内存
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
// 分配内存
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("内存分配失败\n");
return 1;
}
// 初始化并打印
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
printf("%d ", arr[i]);
}
// 释放内存
free(arr);
return 0;
}
2.2 随机数生成
int rand(void):生成一个 0 到 RAND_MAX 之间的随机整数。void srand(unsigned int seed):设置随机数生成的种子。
示例:生成随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL)); // 用当前时间设置随机种子
for (int i = 0; i < 5; i++) {
printf("%d\n", rand() % 100); // 生成 0-99 之间的随机数
}
return 0;
}
2.3 数值转换
int atoi(const char *nptr):将字符串转换为整数。double atof(const char *nptr):将字符串转换为浮点数。long strtol(const char *nptr, char **endptr, int base):将字符串转换为长整数,可以指定基数。
示例:字符串转换为数值
#include <stdio.h>
#include <stdlib.h>
int main() {
char str1[] = "123";
char str2[] = "45.67";
int num1 = atoi(str1); // 转换为整数
double num2 = atof(str2); // 转换为浮点数
printf("num1 = %d, num2 = %.2f\n", num1, num2);
return 0;
}
2.4 排序和查找
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)):快速排序函数。void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)):二分查找函数。
示例:排序数组
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
int main() {
int arr[] = {5, 2, 9, 1, 7};
int n = sizeof(arr) / sizeof(arr[0]);
qsort(arr, n, sizeof(int), compare); // 排序
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
3. <time.h> —— 时间库
<time.h> 提供处理日期和时间的函数。
3.1 时间获取与转换
time_t time(time_t *tloc):返回当前的日历时间(从 1970 年 1 月 1 日的秒数)。struct tm *localtime(const time_t *timer):将time_t类型的时间转换为当地时间的tm结构。char *asctime(const struct tm *timeptr):将tm结构转换为字符串表示的时间。
示例:获取当前时间并格式化输出
#include <stdio.h>
#include <time.h>
int main() {
time_t now;
time(&now); // 获取当前时间
struct tm *local = localtime(&now); // 转换为本地时间
printf("当前时间: %s", asctime(local)); // 格式化输出
return 0;
}
3.2 时间间隔与延时
double difftime(time_t time1, time_t time0):返回time1与time0之间的秒数差。void sleep(unsigned int seconds):暂停执行一段时间(非标准库函数,但常用)。
示例:计算时间差
#include <stdio.h>
#include <time.h>
int main() {
time_t start, end;
start = time(NULL); // 获取起始时间
for (int i = 0; i < 100000000; i++); // 模拟延时
end = time(NULL); // 获取结束时间
double seconds = difftime(end, start);
printf("程序运行时间: %.2f 秒\n", seconds);
return 0;
}
4. 其他常用库
4.1 <string.h> —— 字符串处理库
<string.h> 提供了一系列用于处理字符串的
函数,如字符串复制、连接、比较等。
strcpy、strncpy:字符串复制。strcat、strncat:字符串连接。strcmp、strncmp:字符串比较。strlen:计算字符串长度。strstr:查找子字符串。
示例:字符串操作
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strcat(str1, str2); // 字符串连接
printf("%s\n", str1); // 输出 "HelloWorld"
printf("字符串长度: %zu\n", strlen(str1)); // 计算字符串长度
return 0;
}
4.2 <stdio.h> —— 标准输入输出库
提供了输入输出功能,如 printf、scanf、fopen、fclose、fgetc、fputc 等。
示例:使用 printf 和 scanf
#include <stdio.h>
int main() {
int a;
printf("请输入一个整数: ");
scanf("%d", &a);
printf("你输入的整数是: %d\n", a);
return 0;
}
这些标准库函数可以大大简化 C 语言编程,方便实现常见功能。
评论区
评论列表