c - 猜谜游戏 - 无法弄清楚为什么它会产生一个大的负整数

标签 c

<分区>

大家好。我在使用我生成的这段代码时遇到了问题。它运行一切正常,但是当我猜对它打印数字时,它会打印一个我不明白的数字,如 -3529583。阐明一些光?

#include <stdio.h>
#include <ctype.h>
#include <time.h>

int main()
{
    int y, iRandomNum1; // Declare the three variables
    y = 0;
    iRandomNum1 = 0;
    srand(time(NULL)); // Randomize function
    iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value

    while (iRandomNum1 != y) {
        printf("Guess a number from 1 to 10\n");
        scanf("%d", &y);
    }
    printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
    return 0;
}

最佳答案

您在 printf 中显示变量 &y 的地址而不是变量本身,只需删除 & 符号,它应该是好的

https://stackoverflow.com/users/2173917/sourav-ghosh我在评论中得到了答案

关于c - 猜谜游戏 - 无法弄清楚为什么它会产生一个大的负整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703796/

相关文章:

c - 为什么转换说明符 %o 和 %x 在 C 语言中对 printf() 和 scanf() 的作用不同?

c - 如何在 main 中返回指针到指针的指针?

c - 为什么向链表添加新值会覆盖现有内容?

c - 如何将变量的值组合成 C 中的字符串

C - 大小为 1 的无效读取

c++ - SQLGetPrivateProfileString 错误读取 Unicode 字符

c++ - 取消引用数组与指针

c - 使用结构在C中解析数据包

c - 在 C 中实现详细

c++ - C/C++ 中大文件二进制数据的逻辑操作