C - 整数抛出随机值?

标签 c

目前我正在用 C 编写一个简单的程序,它读取用户在循环中输入的值。出于某种原因,当我初始化整数 a 时,我得到了一个随机值,而不是我指定的值。任何帮助将不胜感激

#include <stdio.h>

int main()
{
    char sName[10];
    int sTime;
    int a = 0;

    printf("%d", &a);

    printf("Please enter the name of your snail: ");
    scanf("%s", &sName);

    for(a = 10; a < 20; a = a + 1) {
        printf("%d", &a);
        printf("Please enter the %d time of your snail: ", &a + 1);
        scanf(" %d ", &sTime);
    }
    return 0;
}

最佳答案

改变这个:

printf("%d", &a);

为此:

printf("%d", a);

&aa的地址(而且是int*类型,所以%d是格式错误)。 a 为您提供 a 的值。

你仍然需要&scanf("%d ", &sTime);; scanf 需要 sTime 的地址,所以它知道在哪里存储值。

关于C - 整数抛出随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141865/

相关文章:

有人可以帮我解释一下吗?

android - 为 armv7 构建 tcc 并在 android 设备上运行它,我应该把头文件放在哪里?

c - 这是删除 C 中链表的正确方法吗?

C : compare 2 chars

c++ - 当从集合中随机给出一个整数时,快速获取整数 0、1 和 2 的方法

c - 将变量参数传递给新创建的进程

c++ - 使用 Autotools (C/C++) 生成可选的源代码?

c++ - 在 64 位操作系统中运行的 32 位应用程序可以寻址的不同内存的数量是多少?

python - 缓冲区溢出 - 插入了意外值

c - openMotif和C编程问题将字符串传输到Dialog Widget