c - 我在这里遇到运行时错误的具体原因是什么?

标签 c

#include<stdio.h>
#include<string.h>
void main()
{
    char a,b,c;
    printf("Enter alien names:\n");
    scanf("%s\n%s\n%s\n",a,b,c);
    printf("The alien names are %s, %s and %s. A meteor hit %s's spaceship. A star scratched %s\'s spaceship. But %s fixed %s and %s\'s spaceships. The three became friends and are from the planet BYG (which means BLUE YELLOW GREEN)",a,b,c,a,b,c,a,b);
}

我在这里遇到运行时错误的具体原因是什么?

最佳答案

要解决这个问题,您应该简单地考虑使用字符串(字符数组)来包含不同的名称。

这是一个如何做到这一点的例子:

    void main()
{
// The string "a" can contain up to 100 symbols (chars).
char a[100];

printf("Enter an alien name:\n");

scanf("%s",a);

printf("The alien name is %s.", a);

}

“char a”和“char a[100]”之间的区别在于,在第一种情况下,变量“a”对应于单个字符,而在第二种情况下,它对应于一个字符串 - 一个字符数组,可以最多包含 100 个字符。

关于c - 我在这里遇到运行时错误的具体原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44893293/

相关文章:

c - 如何使用 C 在 MPI 中发送(MPI_Send)具有指针字段的嵌套结构

更改互斥体的优先级

c - 将数字从文件传递到结构中

c - 退出多线程/多进程 Web 服务器

在C中更改指针字符串数组中的值

c - 在C中查找char的最大值

c - 在UART接口(interface)初始化MFRC522 RFID芯片

c++ - PfCreateInterface 返回错误 120(未实现)

c - 有没有用 C 语言实现 GPS + 加速度计的卡尔曼滤波器?

c - atoi 函数在 for 循环中不起作用