C语言标准库函数详解及示例
C语言标准库提供了一套丰富的函数,涵盖了输入/输出、字符串操作、数学运算、内存管理等多个方面,极大地简化了程序开发。本文将对常用库函数进行分类讲解,并提供相应的示例。
一、 输入/输出函数 (stdio.h)
这些函数用于处理程序的输入和输出操作,例如从键盘读取数据、向屏幕打印数据、读写文件等。
printf()
: 格式化输出到标准输出 (通常是屏幕)。
#include <stdio.h>
int main() {
int age = 30;
char name[] = "Alice";
printf("My name is %s, and I am %d years old.\
", name, age);
return 0;
}
scanf()
: 从标准输入 (通常是键盘) 读取格式化数据。
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You entered: %d\
", age);
return 0;
}
fopen()
,fclose()
,fprintf()
,fscanf()
: 文件操作函数,用于打开、关闭文件以及进行格式化读写文件。
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w"); // 打开文件用于写入
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fprintf(fp, "This text is written to the file.\
");
fclose(fp); // 关闭文件
return 0;
}
二、 字符串操作函数 (string.h)
这些函数用于处理字符串,例如复制、连接、比较、查找等。
strcpy()
: 复制字符串。
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[20];
strcpy(destination, source);
printf("Destination string: %s\
", destination);
return 0;
}
strcat()
: 连接字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);
printf("Concatenated string: %s\
", str1);
return 0;
}
strlen()
: 获取字符串长度。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
size_t len = strlen(str);
printf("String length: %zu\
", len);
return 0;
}
strcmp()
: 比较两个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\
");
} else {
printf("Strings are not equal.\
");
}
return 0;
}
三、 数学函数 (math.h)
这些函数提供各种数学运算,例如三角函数、指数函数、对数函数等。 需要链接数学库 -lm
进行编译。
sqrt()
: 计算平方根。
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
double root = sqrt(num);
printf("Square root of %.2lf is %.2lf\
", num, root);
return 0;
}
sin()
,cos()
,tan()
: 计算正弦、余弦、正切值。
#include <stdio.h>
#include <math.h>
int main() {
double angle = M_PI / 4.0; // 45 degrees
double sin_val = sin(angle);
double cos_val = cos(angle);
double tan_val = tan(angle);
printf("sin(45): %.2lf\
", sin_val);
printf("cos(45): %.2lf\
", cos_val);
printf("tan(45): %.2lf\
", tan_val);
return 0;
}
四、 内存管理函数 (stdlib.h)
这些函数用于动态内存分配和释放。
malloc()
: 分配指定大小的内存块。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int)); // 分配一个int大小的内存
if (ptr == NULL) {
perror("Memory allocation failed");
return 1;
}
*ptr = 10;
printf("Value: %d\
", *ptr);
free(ptr); // 释放内存
return 0;
}
calloc()
: 分配指定数量的元素,每个元素大小相同,并初始化为0.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)calloc(5, sizeof(int)); // 分配5个int,初始化为0
if (arr == NULL) {
perror("Memory allocation failed");
return 1;
}
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\
", i, arr[i]);
}
free(arr);
return 0;
}
free()
: 释放动态分配的内存。 (在malloc()
和calloc()
示例中已展示)
五、 其他常用函数 (stdlib.h, time.h 等)
atoi()
: 将字符串转换为整数。
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "123";
int num = atoi(str);
printf("Converted number: %d\
", num);
return 0;
}
rand()
,srand()
: 生成伪随机数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL)); // 使用当前时间作为随机数种子
for (int i = 0; i < 5; i++) {
int random_number = rand() % 100; // 生成0-99之间的随机数
printf("Random number: %d\
", random_number);
}
return 0;
}
这只是一些常用C标准库函数的示例,还有许多其他函数可供使用。 建议参考C语言标准库文档以获取更完整的函数列表和详细说明。 记住在使用这些函数之前,需要包含相应的头文件。 编译时,对于使用math.h
的程序,需要添加-lm
链接选项。 例如:gcc your_program.c -o your_program -lm