C 编程,绝对初学者指南 - 第 19 章,strcat() 函数代码无法正常工作

标签 c strcat

<分区>

《C 编程 - 绝对初学者指南》第 19 章,示例 2,带有 strcat() 函数的程序:

#include <stdio.h>
#include <string.h>

int main()
{
    char city[15];
    char st[3];
    char fullLocation[18];

    puts("What town do you live in? ");
    gets(city);

    puts("What state do you live in? (2-letter abbreviation)");
    gets(st);

    strcat (fullLocation, city);
    strcat (fullLocation, ", ");
    strcat (fullLocation, st);

    puts("\nYou live in ");
    puts(fullLocation);

    return 0;

}

当我编译这个程序时,它会显示:

编译器在控制台窗口中的输出:

enter image description here

有什么想法或解释为什么会显示这些奇怪的字符吗?

最佳答案

我相信这是因为您没有初始化变量 fullLocation、st 和 city,并且您的代码被垃圾数据欺骗了。

尝试将它们等同于 ={0}

关于C 编程,绝对初学者指南 - 第 19 章,strcat() 函数代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947049/

相关文章:

c - C 中的内联汇编 - 了解编译结果

在 C 编程中将两个字符串合二为一

c - 如何用C语言打印文本文件中的特定行

C语言中的子父关系和继承

c - malloc 放入 "garbage"值

c - char * 的空终止

c - strcat 在第一个字符串的开头粘贴第二个字符串

在没有缓冲区的情况下连接字符串

使用 CPLEX API 的 MEX 文件的编译器错误

c - Directfb 1.6.3 无法设置 Gamma 斜坡 --> 参数无效