C 语言标准库(四)
person c猿人
watch_later 2024-09-21 21:38:57
visibility 421
class unistd,dirent,pthread
bookmark 专栏
当然,C 语言标准库还包括一些其他不太常见但非常有用的库和功能,以下是一些额外的标准库和它们的主要特性:
1. <time.h> —— 时间和日期库
<time.h> 提供了处理时间和日期的功能,包括获取当前时间、格式化时间、计算时间差等。
1.1 常用函数
time_t time(time_t *t):获取当前时间。struct tm *localtime(const time_t *timep):将time_t转换为本地时间。char *asctime(const struct tm *tm):将tm结构体格式化为字符串。double difftime(time_t end, time_t beginning):计算两个时间点之间的差值。
示例:获取当前时间并格式化
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL); // 获取当前时间
struct tm *tm_info = localtime(&t); // 转换为本地时间
char buffer[26];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info); // 格式化时间
printf("当前时间: %s\n", buffer);
return 0;
}
2. <signal.h> —— 信号处理库
<signal.h> 用于处理程序中的信号,允许程序响应来自操作系统或其他进程的信号。
2.1 常用函数
void (*signal(int signum, void (*handler)(int)))(int):设置信号处理函数。int raise(int sig):向自身发送信号。
示例:处理 SIGINT 信号
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void handle_sigint(int sig) {
printf("捕获到 SIGINT 信号 (Ctrl+C)\n");
exit(0);
}
int main() {
signal(SIGINT, handle_sigint); // 设置信号处理函数
while (1) {
printf("程序运行中... (按 Ctrl+C 退出)\n");
sleep(1);
}
return 0;
}
3. <unistd.h> —— POSIX 操作系统接口
虽然不是 C 标准库的一部分,但在类 UNIX 系统中非常常见,提供了与操作系统进行交互的功能。
3.1 常用函数
pid_t fork(void):创建新进程。int execve(const char *filename, char *const argv[], char *const envp[]):执行程序。int sleep(unsigned int seconds):暂停程序执行指定秒数。
示例:创建新进程
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork(); // 创建新进程
if (pid == 0) {
// 子进程
printf("这是子进程\n");
} else if (pid > 0) {
// 父进程
printf("这是父进程,子进程ID: %d\n", pid);
} else {
// 错误处理
perror("创建进程失败");
}
return 0;
}
4. <dirent.h> —— 目录操作库
<dirent.h> 提供了在目录中遍历文件的功能。
4.1 常用类型和函数
DIR *opendir(const char *name):打开目录。struct dirent *readdir(DIR *dirp):读取目录中的下一个条目。int closedir(DIR *dirp):关闭目录。
示例:遍历目录
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
dir = opendir("."); // 打开当前目录
if (dir == NULL) {
perror("无法打开目录");
return 1;
}
while ((entry = readdir(dir)) != NULL) { // 读取目录条目
printf("%s\n", entry->d_name);
}
closedir(dir); // 关闭目录
return 0;
}
5. <pthread.h> —— POSIX 线程库
虽然不是 C 标准库的一部分,但在多线程编程中非常常用。
5.1 常用类型和函数
pthread_t:线程标识符类型。int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg):创建新线程。void pthread_exit(void *retval):终止线程。
示例:创建和管理线程
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("线程执行中...\n");
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_function, NULL); // 创建线程
pthread_join(thread, NULL); // 等待线程结束
printf("主线程结束\n");
return 0;
}
6. <math.h> —— 数学库
虽然已经提过,但值得再提的是 <math.h> 包含了丰富的数学函数,适合各种计算需求。
6.1 常用函数
double sin(double x)、double cos(double x):三角函数。double sqrt(double x):平方根。double pow(double base, double exp):幂运算。double exp(double x)、double log(double x):指数和对数函数。
示例:使用数学函数
#include <stdio.h>
#include <math.h>
int main() {
double value = 9.0;
printf("平方根: %.2f\n", sqrt(value));
printf("2 的 3 次方: %.2f\n", pow(2.0, 3.0));
return 0;
}
这些库和功能补充了 C 语言的标准库,提供了更多的工具和选项来应对不同的编程需求。掌握这些库可以使你在开发中更加灵活和高效。
chat评论区
评论列表
每天进步一点点
whatshot热门文章