C标准库提供了丰富的功能,涵盖了字符串处理、数学计算、文件操作等多个方面。以下是一些高级功能介绍。
字符串处理
strncat
函数
用于将两个字符串连接起来,最多连接 n
个字符。
char dest[100];
char src[] = "Hello, world!";
strncat(dest, src, 5);
strtok
函数
用于将字符串分割成多个部分。
char str[] = "This is a test string.";
char *tokens[10];
char *delimiters[] = { " ", "a", "is", "a", "test", "string", NULL };
tokens[0] = strtok(str, delimiters);
数学计算
sin
和 cos
函数
用于计算正弦和余弦值。
#include <math.h>
double x = M_PI / 6; // 30 degrees
double sinValue = sin(x);
double cosValue = cos(x);
文件操作
fopen
函数
用于打开文件。
FILE *file = fopen("/path/to/file.txt", "r");
if (file == NULL) {
perror("Error opening file");
return -1;
}
fclose
函数
用于关闭文件。
fclose(file);
扩展阅读
更多关于C标准库的信息,请参阅 C 标准库完整指南.