C语言常用的标准库函数

C语言常用的标准库函数

C语言标准库函数详解及示例

C语言标准库提供了一套丰富的函数,涵盖了输入/输出、字符串操作、数学运算、内存管理等多个方面,极大地简化了程序开发。本文将对常用库函数进行分类讲解,并提供相应的示例。

一、 输入/输出函数 (stdio.h)

这些函数用于处理程序的输入和输出操作,例如从键盘读取数据、向屏幕打印数据、读写文件等。

printf(): 格式化输出到标准输出 (通常是屏幕)。

#include

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

int main() {

int age;

printf("Enter your age: ");

scanf("%d", &age);

printf("You entered: %d\

", age);

return 0;

}

fopen(), fclose(), fprintf(), fscanf(): 文件操作函数,用于打开、关闭文件以及进行格式化读写文件。

#include

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

#include

int main() {

char source[] = "Hello";

char destination[20];

strcpy(destination, source);

printf("Destination string: %s\

", destination);

return 0;

}

strcat(): 连接字符串。

#include

#include

int main() {

char str1[50] = "Hello, ";

char str2[] = "world!";

strcat(str1, str2);

printf("Concatenated string: %s\

", str1);

return 0;

}

strlen(): 获取字符串长度。

#include

#include

int main() {

char str[] = "Hello";

size_t len = strlen(str);

printf("String length: %zu\

", len);

return 0;

}

strcmp(): 比较两个字符串。

#include

#include

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

#include

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

#include

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

#include

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

#include

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

#include

int main() {

char str[] = "123";

int num = atoi(str);

printf("Converted number: %d\

", num);

return 0;

}

rand(), srand(): 生成伪随机数。

#include

#include

#include

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

相关推荐

铃铛教育app
365不给提款怎么办

铃铛教育app

📅 07-03 👁️ 7911
这届世界杯开幕式,亮点有哪些?
365bet客户端

这届世界杯开幕式,亮点有哪些?

📅 06-29 👁️ 1416
灌襞怎么读,灌襞什么意思,灌襞造句,灌襞解释,汉语词典
365bet足球即时比分

灌襞怎么读,灌襞什么意思,灌襞造句,灌襞解释,汉语词典

📅 07-05 👁️ 2209
桌面精灵大全-桌面精灵哪个好
365bet客户端

桌面精灵大全-桌面精灵哪个好

📅 07-10 👁️ 3652
可售马匹
365不给提款怎么办

可售马匹

📅 07-01 👁️ 3009
Nintendo eShop支援
365bet足球即时比分

Nintendo eShop支援

📅 06-30 👁️ 8215
揭秘美国平板电脑价格:性价比大比拼,如何选最合适?
365不给提款怎么办

揭秘美国平板电脑价格:性价比大比拼,如何选最合适?

📅 07-04 👁️ 3442
【恒久相守曜红莲】情倾百年声望玩法解析
365bet客户端

【恒久相守曜红莲】情倾百年声望玩法解析

📅 07-03 👁️ 8517