向着白帽子进发 2019-05-16 17:27 采纳率: 100%
浏览 2021
已采纳

VS2017环境下使用strcat函数问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char c[50] = { "People's Republic of'\0'" };
    char str[] = { "China" };
    printf("%s\n", strcat_s(c, str));
    system("pause");
    return 0;
}

图片说明

使用strcat函数是连接两个字符组。
那么运行结果应该是:People's Republic of China
为何出现的黑框是如图结果???
求大佬解答QAQ

  • 写回答

3条回答 默认 最新

  • 北冥有鱼wyh 2019-05-17 08:23
    关注

    正确代码

    #include "pch.h"
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
        char c[50] = { "People's Republic of " };
        char str[] = { "China" };
        strcat_s(c, str);
        printf("%s\n", c);
        system("pause");
    
        return 0;
    }
    

    运行结果

    图片说明

    错误总结

    1、对于char c[50] = { "People's Republic of'\0'" };,字符串c并不需要写出'\0',因为系统编译时运行默认自动在字符串结尾处添加'\0';
    2、strcat_s(c, str)默认返回类型是errno_t,你输出strcat_s(c, str)相对于输出errno_t类型的结果,由此出现上述错误结果;
    3、未正确理解strcat_s(c, str)功能。其真正功能是将str连接到c的结尾处,故其结果是在c字符串中,而不是在返回值中。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?