C语言函数大全
本篇介绍C语言函数大全-- m 开头的函数
1. malloc
1.1 函数说明
函数声明 | 函数功能 |
---|---|
void *malloc(size_t size); |
用于动态分配内存 |
参数:
- size : 需要分配的内存大小(以字节为单位)
返回值:
- 如果分配成功,返回分配的内存块的指针;
- 如果分配失败,则返回 NULL。
1.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = NULL;
// 分配内存
str = (char *)malloc(20 * sizeof(char));
if (str == NULL)
{
printf("Failed to allocate memory.\n");
return 1;
}
// 将字符串复制到内存中
strcpy(str, "Hello, world!");
// 输出字符串
printf("%s\n", str);
// 释放内存
free(str);
return 0;
}
在上面的示例程序中,
- 我们首先声明一个指向字符型的指针
str
,并将其初始化为NULL
; - 然后使用
malloc()
函数动态分配了 20 字节的内存空间,并将其赋值给str
指针; - 接下来,我们使用
strcpy()
函数将字符串"Hello, world!"
复制到内存中,并使用printf()
函数输出字符串; - 最后,我们使用
free()
函数释放了分配的内存空间。
1.3 运行结果
2. mblen
2.1 函数说明
函数声明 | 函数功能 |
---|---|
int mblen(const char *s, size_t n); |
检查多字节字符的长度 |
参数:
- s : 指向待检查的多字节字符或多字节字符序列的指针
- n : 要检查的最大字节数
注意:如果 s
是空指针,则返回 0,表示不是多字节字符;否则,如果 n
不足以包含完整的多字节字符,则返回 -1,表示需要更多的输入;否则,返回多字节字符所需的字节数。
2.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main()
{
// 设置本地化环境
setlocale(LC_ALL, "");
char str[] = u8"你好,世界!";
int len;
// 检查第一个字符的长度
len = mblen(str, MB_CUR_MAX);
if (len == -1)
{
printf("Failed to determine the length of the multibyte character.\n");
return 1;
}
printf("The length of the first multibyte character is %d bytes.\n", len);
return 0;
}
在上面的示例程序中,
- 我们首先使用
setlocale()
函数设置本地化环境,以便正确处理多字节字符。 - 然后我们定义了一个包含中文字符的字符串
str
; - 接着使用
mblen()
函数检查第一个字符的长度,并将其保存到变量len
中。 - 最后,我们输出该字符的长度。
2.3 运行结果
3. mbrlen
3.1 函数说明
函数声明 | 函数功能 |
---|---|
size_t mbrlen(const char *s, size_t n, mbstate_t *ps); |
检查多字节字符的长度 |
参数:
- s : 指向待检查的多字节字符或多字节字符序列的指针
- n : 要检查的最大字节数
- ps : 描述转换状态的
mbstate_t
结构体的指针
注意: 如果 s
是空指针,则返回 0,表示不是多字节字符;否则,如果 n
不足以包含完整的多字节字符,则返回 (size_t)-2
,表示需要更多的输入;否则,如果 ps
是 NULL
,则使用默认转换状态;否则,将 ps
的值更新为已经转换的字符数,并返回多字节字符所需的字节数。
3.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
int main()
{
// 设置本地化环境
setlocale(LC_ALL, "");
char str[] = u8"你好,世界!";
int len;
// 检查第一个字符的长度
len = mbrlen(str, MB_CUR_MAX, NULL);
if (len == (size_t)-2) // 特殊的返回值,表示发生了错误
{
printf("Failed to determine the length of the multibyte character.\n");
return 1;
}
printf("The length of the first multibyte character is %d bytes.\n", len);
return 0;
}
在上面的示例程序中,
- 我们首先使用
setlocale()
函数设置本地化环境,以便正确处理多字节字符。 - 然后我们定义了一个包含中文字符的字符串
str
; - 接着使用
mbrlen()
函数检查第一个字符的长度,并将其保存到变量len
中。 - 最后,我们输出该字符的长度。
3.3 运行结果
4. mbrtowc
4.1 函数说明
函数声明 | 函数功能 |
---|---|
size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps); |
将多字节字符转换为宽字符 |
参数:
- pwc : 一个指向宽字符的指针,表示将要存入转换后的宽字符;
- s : 一个指向多字节字符或字符序列的指针;
- n : 一个表示最多转换的字节数的整数;
- ps : 一个指向转换状态的指针,如果为 NULL,则使用默认转换状态。
返回值:
- 如果能转换,返回转换的字符数;
- 如果不能转换,则返回
(size_t)-1
。
4.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
int main()
{
// 设置本地化环境
setlocale(LC_ALL, "");
char str[] = u8"你好,世界!";
wchar_t wc;
mbstate_t state = {0};
// 将第一个字符转换为宽字符
size_t len = mbrtowc(&wc, str, MB_CUR_MAX, &state);
if (len == (size_t)-1)
{
printf("Failed to convert multibyte character.\n");
return 1;
}
// 输出宽字符
wprintf(L"The first wide character is: %lc\n", wc);
return 0;
}
在上面的示例程序中,
- 我们首先使用
setlocale()
函数设置本地化环境,以便正确处理多字节字符。 - 然后我们定义了一个包含中文字符的字符串
str
; - 接着使用
mbrtowc()
函数将第一个字符转换为宽字符,并将其保存到变量wc
中。 - 最后,我们使用
wprintf()
函数输出宽字符。
注意: 在调用 mbrtowc()
函数之前,必须将 mbstate_t
结构体的值初始化为 0
。在 C99 标准中,可以使用大括号对结构体进行初始化,这会把结构体或数组的每个元素都初始化为默认值(0
或 NULL
)。
4.3 运行结果
5. mbsinit
5.1 函数说明
函数声明 | 函数功能 |
---|---|
int mbsinit(const mbstate_t *ps); |
检查转换状态是否为起始状态 |
参数:
- ps : 指向
mbstate_t
结构体的指针,表示要检查的转换状态。
注意: 如果 ps
是空指针,则返回非零值(真),表示默认转换状态已经初始化;否则,如果 ps
描述的转换状态是起始状态,则返回非零值(真);否则,返回 0
(假)。
5.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
int main()
{
// 设置本地化环境
setlocale(LC_ALL, "");
char str[] = u8"你好,世界!";
mbstate_t state = {0};
// 检查转换状态是否为起始状态
if (!mbsinit(&state))
{
printf("The conversion state is not initial.\n");
return 1;
}
// 转换第一个字符
size_t len = mbrlen(str, MB_CUR_MAX, &state);
if (len == (size_t)-2)
{
printf("Failed to determine the length of the multibyte character.\n");
return 1;
}
// 检查转换状态是否改变
if (mbsinit(&state))
{
printf("The conversion state did not change.\n");
return 1;
}
printf("The first multibyte character is %d bytes long.\n", len);
return 0;
}
5.3 运行结果
参考
- [API Reference Document]