strncpy 和 snprintf 的区别

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;
}

相关推荐

  1. strncpy snprintf 区别

    2024-07-18 08:06:01       22 阅读
  2. strcpystrncpy函数详解

    2024-07-18 08:06:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 08:06:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 08:06:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 08:06:01       57 阅读
  4. Python语言-面向对象

    2024-07-18 08:06:01       68 阅读

热门阅读

  1. Kafka系列之:Kafka存储数据相关重要参数理解

    2024-07-18 08:06:01       17 阅读
  2. Oracle(8)什么是Oracle实例(Instance)?

    2024-07-18 08:06:01       22 阅读
  3. python 迭代器介绍 map() 函数

    2024-07-18 08:06:01       18 阅读
  4. Linux chmod 命令简介

    2024-07-18 08:06:01       24 阅读
  5. QT+winodow 代码适配调试总结(三)

    2024-07-18 08:06:01       20 阅读
  6. 代码随想录学习 54day 图论 A star算法

    2024-07-18 08:06:01       16 阅读
  7. Windows中配置Python 3.11环境安装教程

    2024-07-18 08:06:01       22 阅读
  8. 灵动微单片机SPI LCD彩屏方案优势

    2024-07-18 08:06:01       22 阅读
  9. DP学习——组合模式

    2024-07-18 08:06:01       18 阅读
  10. (三)js前端开发中设计模式之工厂模式

    2024-07-18 08:06:01       18 阅读