C ( C99 & C11 )
文中的部分函数与头文件只有 C99 & C11 及更高标准中才能使用, 因此若编译器支持的 C 语言标准过低 ( 如 vc 6.0, turbo - C 等只支持 C89 标准 ) 请更换能支持更高标准的编译器 (dev-cpp, visual studio 13及更高版本, gcc, clang等)
字符串处理函数
fgets和fputs
因为 gets 不安全, 所以在一些编译器中 gets 无法使用. 于是用 fgets 和 fputs 代替
fgets的函数原型 :
char *__cdecl fgets(char *_Buffer, int _MaxCount, FILE *_Stream)
函数参数从左至右依次是 : 指向字符数组的指针, 用来存储字符串. 字符串的最大长度, 输入/输出流
fputs的函数原型 :
int __cdecl fputs(const char *_Buffer, FILE *_Stream)
函数参数从左至右依次是 : 指向字符数组的指针, 输入/输出流
#include <stdio.h>
int main(void)
{
char ch[40];
fgets(ch, 40, stdin); /*输入Hello, World!*/
fputs(ch, stdout); /*输出Hello, World!, 并在字符串末尾带上换行符*/
return 0;
}
输出结果
Hello, World!
scanf 与 fgets :
- fgets 与 scanf 最大的不同就在于 scanf 遇到空白符 (如空格回车等) 就会停止读取字符. 而 fgets 不会, 它遇到换行符或者已达读取字符数量数量上限时才会停止读取.
- 如果fgets读取的字符数量超过上限, 则超过上限的字符会被保留在缓冲区中
- fgets 会读取换行符 '\n', 因此这就是为什么 fgets 读取的字符数量要少 1 个.
下面是一些常用的 C 语言中有关字符串操作相关的函数, 大部分包含于头文件 <string.h> 中.
strlen()
函数原型 :
size_t strlen(const char *_Str)
顾名思义, 用来返回字符串长度的. 函数参数是 指向字符数组的指针
#include <stdio.h>
#include <string.h>
int main(void)
{
char ch[40];
fgets(ch, 40, stdin); /*输入Hello, World!*/
printf("%d\n", strlen(ch));
return 0;
}
输出结果
14
strlen 返回的由 fgets 输入的字符串的字符数时, 一般情况下比实际字符数要多 1, 因为 strlen 会将换行符也读取进去.
strcat()
函数原型 :
char * strcat(char *_Destination, const char *_Source)
用于拼接字符串, 即将原字符串插入目标字符串的后面. 函数参数从左到右依次是 : 指向目标字符数组的指针, 指向原字符数组的指针. 返回类型是一个 char 指针*.
#include <stdio.h>
#include <string.h>
int main(void)
{
char ch[40];
char CH[40];
scanf("%s", ch); /*输入Hello, */
scanf("%s", CH); /*输入World!*/
strcat(ch, CH);
printf("%s", ch);
return 0;
}
输出结果
Hello, World!
strcmp
函数原型 :
int strcmp(const char *_Str1, const char *_Str2)
顾名思义用于比较字符串的长短或者查看两个字符串是否相等. 其原理是将字符串的字符一一比较 ascii 值大小得到的.
#include <stdio.h>
#include <string.h>
int main(void)
{
char A[40] = "Hello, World!";
char B[40] = "hello, world!";
char C[40] = "Hello, World";
printf("%d\n", strcmp(A, B));
printf("%d\n", strcmp(A, C));
printf("%d\n", strcmp(A, A));
return 0;
}
输出结果
-1
1
0
一般来说如果返回值为正, 则字符串一 \(>\) 字符串二, 等于 1 则说明两个字符串相等, 为负则说明字符串一 \(<\) 字符串二
strcpy()
函数原型 :
char * strcpy(char *_Destination, const char *_Source)
顾名思义用于复制字符串, 将原字符串覆盖目标字符串. 函数参数从左至右依次是 指向目标字符数组的指针, 指向原字符数组的指针.
#include <stdio.h>
#include <string.h>
int main(void)
{
char ch[40] = "Hello, World!";
char CH[40] = "World!";
strcpy(ch, CH);
printf("%s\n", ch);
return 0;
}
输出结果
World!
或者是覆盖某一部分, 不用全覆盖
#include <stdio.h>
#include <string.h>
int main(void)
{
char ch[40] = "Hello, World!";
char CH[40] = "World!";
strcpy(ch + 3, CH);
printf("%s\n", ch);
return 0;
}
输出结果
HelWorld!
内存管理函数
C语言中最常用的就是 malloc 和 free 函数了, 这两个函数包含于头文件 <malloc.h> 或者 <stdlib.h> 中.
malloc()和free()
函数原型 :
void * malloc(size_t _Size)
malloc 向内存申请一块空间, 大小为 _Size, 并返回一个指向该空间的地址. 指针类型和内存大小由程序员自行定义.
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
{
int data;
struct LinkNode *next;
} LinkNode;
int main(void)
{
LinkNode *List = (LinkNode *)malloc(sizeof(LinkNode));
List->data = 1;
List->next = (LinkNode *)malloc(sizeof(LinkNode));
return 0;
}
free()函数用于释放内存, 只需向函数传递已经通过 malloc 分配空间的指针即可.
函数原型 :
void free(void *_Block)
数学函数
C语言中提供了许多数学函数, 它们大多包含在头文件 <math.h> 中
fabs()
函数原型 :
double fabs(double _X)
常用于对 double 类型的变量取绝对值, 返回值的类型为 double.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = -3.14159;
printf("%lf\n", abs(x));
return 0;
}
输出结果
3.141590
floor()和ceil
floor()
函数原型 :
double floor(double _X)
用来对 double 变量向下取整
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 3.14159;
printf("%lf\n", floor(x));
return 0;
}
输出结果
3.000000
ceil()
函数原型 :
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 3.14159;
printf("%lf\n", ceil(x));
return 0;
}
用来对 double 变量向上取整
输出结果
4.000000
pow()
函数原型 :
double pow(double _X, double _Y)
用来求 double 变量的幂, 函数参数从左至右依次为 : 底数, 指数
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 3.14159;
printf("%lf\n", pow(x, 3));
return 0;
}
输出结果
31.006198
sqrt()
函数原型 :
double sqrt(double _X)
用于求 double 变量的平方根
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 4.00;
printf("%lf\n", sqrt(x));
return 0;
}
输出结果
2.000000
cbert()
函数原型 :
double cbrt(double _X)
用来求 double 变量的立方根
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 27.00;
printf("%lf\n", cbrt(x));
return 0;
}
输出结果
3.000000
log()
函数原型 :
double __cdecl log(double _X)
用于求 double 变量以自然对数为底的对数
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf\n", log(1));
return 0;
}
输出结果
0.000000
log10()
函数原型 :
double __cdecl log10(double _X)
用于求 double 变量的以10为底的对数
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf\n", log10(10));
return 0;
}
输出结果
1.000000
exp()
函数原型 :
double exp(double _X)
用来求以e为底的幂
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf\n", exp(1));
return 0;
}
输出结果
2.718282
sin()和asin()
sin()
函数原型 :
double sin(double _X)
用于计算正弦值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", sin(1.5707963)); /*1.5707963 即 π/2*/
return 0;
}
输出结果
1.000000
asin()
函数原型 :
double asin(double _X)
用于计算反正弦值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", asin(1));
return 0;
}
输出结果
1.570796
cos()和acos()
cos()
函数原型 :
double cos(double _X)
用于计算余弦值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", cos(1));
return 0;
}
输出结果
0.540302
acos()
函数原型 :
double __cdecl acos(double _X)
用于计算反余弦值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", acos(0.5));
return 0;
}
输出结果
1.047198
tan()和atan()
tan()
函数原型 :
double __cdecl tan(double _X)
用于计算正切值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", tan(3.1415926 / 3));
return 0;
}
输出结果
1.732051
atan()
函数原型 :
double __cdecl atan(double _X)
用于计算反正切值
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", atan(3.1415926));
return 0;
}
输出结果
1.262627
round()
函数原型 :
double round(double _X)
用于对浮点数进行四舍五入
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf", round(4.5));
return 0;
}
输出结果
5.000000
其他函数
stdlib.h
atof()
函数原型 :
double atof(const char *_String)
主要用于将字符串中的数字转化为为 double 浮点值, 读取时遇到空白符会跳过, 遇到非数字字符会直接结束转换
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char A[10] = "1.2345";
char B[10] = " 1.2345";
char C[10] = "1.23a45";
printf("%lf\n", atof(A));
printf("%lf\n", atof(B));
printf("%lf\n", atof(C));
return 0;
}
输出结果
1.234500
1.234500
1.230000
atoi
函数原型 :
int __cdecl atoi(const char *_String)
主要用于将字符串中的数字转化为 int 值, 读取时遇到空白符会跳过, 遇到非数字字符会直接结束转换
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char A[10] = "1234";
char B[10] = " 1234";
char C[10] = "1.23a4";
printf("%d\n", atoi(A));
printf("%d\n", atoi(B));
printf("%d\n", atoi(C));
return 0;
}
输出结果
1234
1234
1
atol()
函数原型 :
long __cdecl atol(const char *_String)
主要用于将字符串中的数字转化为 long 值, 读取遇到空白符会跳过, 遇到非数字字符会直接结束转换
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char A[10] = "1234";
char B[10] = " 1234";
char C[10] = "1.23a4";
printf("%ld\n", atol(A));
printf("%ld\n", atol(B));
printf("%ld\n", atol(C));
return 0;
}
输出结果
1234
1234
1
rand()
函数原型 :
int rand(void)
顾名思义, 它主要用来输出随机数的, 大多配合循环或递归使用
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for (int i = 0; i < 5; i++)
{
int x = rand();
printf("%d\n", x);
}
return 0;
}
输出结果
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for (int i = 0; i < 5; i++)
{
int x = rand();
printf("%d\n", x);
}
return 0;
}
abs()
函数原型 :
long long abs(const long long _X)
主要用来求整数的绝对值, 返回类型和函数参数类型都为 long long.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%ld\n", abs(-12345));
return 0;
}
输出结果
12345
ctype.h
判断字符串中的字符函数
函数 | 描述 |
---|---|
isalnum() | 是否为字母数字 |
isalpha() | 是否为字母 |
islower() | 是否为小写字母 |
isupper() | 是否为大写字母 |
isdigit() | 是否为数字 |
isxdigit() | 是否为16进制数字 |
iscntrl() | 是否为控制字符 |
isgraph() | 是否为图形字符(例如,空格、控制字符都不是) |
isspace() | 是否为空格字符(包括制表符、回车符、换行符等) |
isblank() | 是否为空白字符([C99]/[C11]新增)(包括水平制表符) |
isprint() | 是否为可打印字符 |
ispunct() | 是否为标点 |
tolower() | 转换为小写 |
toupper() | 转换为大写 |
stdbool.h
宏名称 | 展开 |
---|---|
bool | _Bool |
true | 整数常量1 |
false | 整数常量0 |
__bool_true_false_are_defined | 整数常量1 |