在C语言编程的世界里,函数是构建程序的基本单元。掌握一些实用的函数对于提高编程效率和理解程序逻辑至关重要。本文将详细介绍200个C语言中的实用函数,并分享一些应用技巧,帮助读者在编程旅途中更加得心应手。
一、基础输入输出函数
1. printf 和 scanf
这两个函数是C语言中最基本的输入输出函数。
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
2. getchar 和 putchar
getchar 用于从标准输入读取一个字符,而 putchar 用于将一个字符输出到标准输出。
#include <stdio.h>
int main() {
char ch;
ch = getchar();
putchar(ch);
return 0;
}
二、字符串处理函数
1. strlen
用于计算字符串的长度。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %lu\n", strlen(str));
return 0;
}
2. strcpy 和 strncpy
用于复制字符串。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Source string";
char dest[20];
strcpy(dest, src);
printf("Destination string: %s\n", dest);
return 0;
}
三、数学函数
1. sin, cos, tan
用于计算三角函数值。
#include <stdio.h>
#include <math.h>
int main() {
double angle = 45.0;
printf("sin(45): %f\n", sin(angle * M_PI / 180.0));
printf("cos(45): %f\n", cos(angle * M_PI / 180.0));
printf("tan(45): %f\n", tan(angle * M_PI / 180.0));
return 0;
}
2. sqrt
用于计算平方根。
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
printf("Square root of %f: %f\n", num, sqrt(num));
return 0;
}
四、时间处理函数
1. time
用于获取当前时间。
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
printf("Current time: %s", ctime(&t));
return 0;
}
2. localtime
用于将时间转换为本地时间。
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *tm_info = localtime(&t);
printf("Local time: %s", asctime(tm_info));
return 0;
}
五、内存管理函数
1. malloc, calloc, realloc
用于动态分配和调整内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(10 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// 使用 ptr
free(ptr);
return 0;
}
2. memcpy, memmove
用于复制内存块。
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Source string";
char dest[20];
memcpy(dest, src, strlen(src) + 1);
printf("Destination string: %s\n", dest);
return 0;
}
六、文件操作函数
1. fopen, fclose
用于打开和关闭文件。
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("File opening failed\n");
return 1;
}
fprintf(file, "Hello, World!");
fclose(file);
return 0;
}
2. fread, fwrite
用于读写文件内容。
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("File opening failed\n");
return 1;
}
char buffer[100];
fread(buffer, sizeof(char), 100, file);
printf("File content: %s\n", buffer);
fclose(file);
return 0;
}
七、其他实用函数
1. atoi, atol, atoll
用于将字符串转换为整数。
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("Converted integer: %d\n", num);
return 0;
}
2. strcmp, strncmp
用于比较字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
printf("Comparison result: %d\n", result);
return 0;
}
八、总结
本文详细介绍了200个C语言中的实用函数,包括基础输入输出、字符串处理、数学函数、时间处理、内存管理、文件操作以及其他实用函数。通过学习和掌握这些函数,读者可以在C语言编程的道路上更加得心应手。希望本文能对您的编程之旅有所帮助。
