1. strncpy 和 snprintf 的区别
strncpy 和 snprintf 的区别尾部是否添加 ‘\0’ 字符问题
1.1. 结论
char * strncpy ( char * destination, const char * source, size_t num );
- 从 source 拷贝 num 个字符到 destination,source 和 destination 的地址不能重叠,有重叠请使用 memmove,momcpy也不支持哦
- 如果 source 的长度小于 num,则拷贝 source 的全部字符,其他字符补 ‘\0’
- [坑]如果 source 的长度大于 num,则拷贝 num 个字符到 destination,无 ‘\0’ 字符,所以不是标准字符串,不可使用 strlen 等函数
- strncpy_s 可以避坑,但是只有windows支持,linux不支持
int snprintf ( char * s, size_t n, const char * format, ... );
- 从 source 拷贝 num 个字符到 destination,source 和 destination 的地址不能重叠,有重叠请使用 memmove,momcpy也不支持哦
- 【注意】如果 source 的长度小于 num-1,则拷贝 source 的全部字符, 只补充一个 ‘\0’ 字符
- 如果 source 的长度大于 num-1,则拷贝 num-1 个字符到 destination,最后字符补 ‘\0’ 字符
- 正确填充的话,s是标准字符串,可以调用 strlen 等函数
1.2. demo
#include <stdio.h>
#include <string.h>
// 因为 `\0` 无法打印,这里使用 F 代表 `\0` 字符,方便查看
void print_char(char* data, int len) {
for (size_t i = 0; i < len; i++) {
if (data[i] == '\0') {
printf("%c", 'F');
}
printf("%c", data[i]);
}
}
#define FUNC_COUNT 4
#define CP_COUNT 5
const char* long_src = "123456789";
const char* short_src = "12";
int main() {
char dst[FUNC_COUNT][CP_COUNT + 2];
memset(dst, 'a', sizeof(dst));
strncpy(dst[0], long_src, CP_COUNT);
printf("\nstrncpy[%lu==>%d] ----- ", strlen(long_src), CP_COUNT);
print_char(dst[0], CP_COUNT + 2); // strncpy[9==>5] ----- 12345aa
strncpy(dst[1], short_src, CP_COUNT);
printf("\nstrncpy[%lu==>%d] ----- ", strlen(short_src), CP_COUNT);
print_char(dst[1], CP_COUNT + 2); // strncpy[2==>5] ----- 12FFFaa
int w_count = snprintf(dst[2], CP_COUNT, "%s", long_src);
printf("\nsnprintf[%lu==>%d]=[%d] ----- ", strlen(long_src), CP_COUNT, w_count);
print_char(dst[2], CP_COUNT + 2); // snprintf[9==>5]=[9] ----- 1234Faa
w_count = snprintf(dst[3], CP_COUNT, "%s", short_src);
printf("\nsnprintf[%lu==>%d]=[%d] ----- ", strlen(short_src), CP_COUNT, w_count);
print_char(dst[3], CP_COUNT + 2); // snprintf[2==>5]=[2] ----- 12Faaaa
return 0;
}