C语言常用库函数
C语言是一种广泛使用的编程语言,它提供了丰富的库函数,这些库函数可以帮助程序员完成各种任务,从简单的数学运算到复杂的文件操作。本教程将介绍一些C语言中常用的库函数,包括数学库、字符串处理库、标准输入输出库和时间处理库。
1. 数学库(math.h)
数学库提供了一系列的数学函数,用于执行各种数学运算。

1.1 基础数学函数
double sin(double x);:计算x(以弧度为单位)的正弦值。double cos(double x);:计算x(以弧度为单位)的余弦值。double tan(double x);:计算x(以弧度为单位)的正切值。double sqrt(double x);:计算x的平方根。double pow(double base, double exponent);:计算base的exponent次幂。
C
#include <stdio.h>
#include <math.h>
int main() {
double angle = M_PI / 4; // 45度转换为弧度
printf("sin(45°) = %f\n", sin(angle));
printf("cos(45°) = %f\n", cos(angle));
printf("tan(45°) = %f\n", tan(angle));
printf("sqrt(16) = %f\n", sqrt(16));
printf("2^3 = %f\n", pow(2, 3));
return 0;
}1.2 圆周率和自然对数
double M_PI;:圆周率π的值。double M_E;:自然对数的底e的值。double log(double x);:计算x的自然对数。double log10(double x);:计算x的以10为底的对数。
C
#include <stdio.h>
#include <math.h>
int main() {
printf("Pi = %f\n", M_PI);
printf("e = %f\n", M_E);
printf("log(10) = %f\n", log(10));
printf("log10(100) = %f\n", log10(100));
return 0;
}2. 字符串处理库(string.h)
字符串处理库提供了一系列的函数,用于操作C语言中的字符串。
2.1 字符串复制
char *strcpy(char *dest, const char *src);:将src指向的字符串复制到dest指向的位置。char *strncpy(char *dest, const char *src, size_t n);:将src指向的字符串的前n个字符复制到dest指向的位置。
C
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[20];
strcpy(dest, src);
printf("strcpy: %s\n", dest);
strncpy(dest, src, 5);
printf("strncpy: %s\n", dest);
return 0;
}2.2 字符串连接
char *strcat(char *dest, const char *src);:将src指向的字符串连接到dest指向的字符串的末尾。char *strncat(char *dest, const char *src, size_t n);:将src指向的字符串的前n个字符连接到dest指向的字符串的末尾。
C
#include <stdio.h>
#include <string.h>
int main() {
char dest[] = "Hello, ";
char src[] = "World!";
strcat(dest, src);
printf("strcat: %s\n", dest);
strncat(dest, src, 3);
printf("strncat: %s\n", dest);
return 0;
}3. 标准输入输出库(stdio.h)
标准输入输出库提供了一系列的函数,用于执行输入输出操作。
3.1 格式化输入输出
int printf(const char *format, ...);:格式化输出到标准输出。int scanf(const char *format, ...);:格式化输入。
C
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);
return 0;
}3.2 文件操作
FILE *fopen(const char *filename, const char *mode);:打开文件。int fclose(FILE *stream);:关闭文件。int fprintf(FILE *stream, const char *format, ...);:格式化输出到文件。int fscanf(FILE *stream, const char *format, ...);:格式化输入。
C
#include <stdio.h>
int main() {
FILE *file;
int a, b;
file = fopen("data.txt", "r");
if (file == NULL) {
perror("Error opening file");
return -1;
}
fscanf(file, "%d %d", &a, &b);
fclose(file);
printf("Sum: %d\n", a + b);
return 0;
}4. 时间处理库(time.h)
时间处理库提供了一系列的函数,用于处理时间。
4.1 获取当前时间
time_t time(time_t *t);:获取当前时间。
C
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Current time: %s", asctime(timeinfo));
return 0;
}4.2 格式化时间
char *asctime(const struct tm *timeptr);:将tm结构体转换为字符串。
C
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Formatted time: %s", asctime(timeinfo));
return 0;
}在基础的C语言库函数之上,还有一些进阶的库函数,这些函数可以帮助你处理更复杂的数据结构、执行更高级的数学运算、以及进行更细致的系统操作。本教程将介绍一些进阶的C语言库函数,包括动态内存管理、高级数学函数、文件操作和多线程编程。
5. 动态内存管理(stdlib.h)
动态内存管理是C语言中非常重要的一部分,它允许程序在运行时分配和释放内存。
5.1 内存分配
void *malloc(size_t size);:分配指定大小的内存块。void *calloc(size_t nmemb, size_t size);:分配内存并初始化为0。void *realloc(void *ptr, size_t new_size);:更改先前分配的内存块的大小。
C
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
fprintf(stderr, "Error allocating memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i * i;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}5.2 内存释放
void free(void *ptr);:释放先前分配的内存。
注意:在使用完动态分配的内存后,应该使用free函数释放它,以避免内存泄漏。
6. 高级数学函数(math.h)
除了基础的数学函数外,math.h还提供了一些高级的数学函数。
6.1 双曲函数
double sinh(double x);:计算x的双曲正弦值。double cosh(double x);:计算x的双曲余弦值。double tanh(double x);:计算x的双曲正切值。
C
#include <stdio.h>
#include <math.h>
int main() {
printf("sinh(1) = %f\n", sinh(1));
printf("cosh(1) = %f\n", cosh(1));
printf("tanh(1) = %f\n", tanh(1));
return 0;
}6.2 特殊函数
double exp(double x);:计算e的x次幂。double log2(double x);:计算x的以2为底的对数。
C
#include <stdio.h>
#include <math.h>
int main() {
printf("exp(1) = %f\n", exp(1));
printf("log2(8) = %f\n", log2(8));
return 0;
}7. 文件操作进阶(stdio.h)
除了基础的文件操作外,还有一些进阶的文件操作函数。
7.1 文件定位
int fseek(FILE *stream, long offset, int whence);:设置文件位置指针。long ftell(FILE *stream);:获取当前文件位置。
C
#include <stdio.h>
int main() {
FILE *file;
file = fopen("example.txt", "r+");
if (file == NULL) {
perror("Error opening file");
return -1;
}
fseek(file, 10, SEEK_SET); // 移动到文件开头后的第10个字节
char buffer[100];
fgets(buffer, 100, file);
printf("%s", buffer);
fclose(file);
return 0;
}7.2 文件锁定
int flock(int fd, int operation);:对文件进行加锁或解锁。
C
#include <stdio.h>
#include <sys/file.h>
int main() {
int fd = open("example.txt", O_RDWR);
if (fd == -1) {
perror("Error opening file");
return -1;
}
flock(fd, LOCK_EX); // 独占锁定
// 执行文件操作
flock(fd, LOCK_UN); // 解锁
close(fd);
return 0;
}8. 多线程编程(pthread.h)
多线程编程允许程序同时执行多个任务。
8.1 创建和同步线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);:创建新线程。void pthread_exit(void *retval);:结束线程。int pthread_join(pthread_t thread, void **retval);:等待线程结束。
C
#include <stdio.h>
#include <pthread.h>
void *print_hello(void *arg) {
printf("Hello from thread %ld\n", pthread_self());
return NULL;
}
int main() {
pthread_t tid;
if (pthread_create(&tid, NULL, print_hello, NULL) != 0) {
perror("Failed to create thread");
return 1;
}
pthread_join(tid, NULL);
printf("Main thread ending\n");
return 0;
}上述这些C语言库函数非常重要,通过学习掌握这些库函数,你可以编写更高效、更复杂的C程序。