c - 为什么声明变量后我得到不同的数字?

标签 c

我是 C 编程新手,我试图编写一个简单的程序,要求用户重新排列屏幕上显示的数字,但我遇到了问题,我会在屏幕上打印出不同的数字我分配给变量的值。为什么会得到不同的数字?这是我遇到的问题和我的代码的屏幕截图 the image :

#include <stdio.h>
main()
{
int numOne, numTwo, numThree, ansOne, ansTwo, ansThree;
char name[20];
numOne=34521;

printf("\nWelcome to scrambled numbers Game");
printf("\n Please input your name to get started: ");
scanf("%s", name);
printf("\nRe-arrange this numbers in ascending order %d :", &numOne);
scanf("%d", &ansOne);

if(ansOne==12345)
{
    printf("Congratulations %s you have won the first round", name);
}
else
{
    printf("sorry %s you failed the first round", name);
}

}

最佳答案

发生这种情况是因为您正在打印变量 numOne 的地址而不是 numOne 本身。

尝试从 printf 中的 numOne 中删除 & 运算符。

替换这个: printf("\n按升序重新排列这些数字 %d :", &numOne);

通过这个: printf("\n按升序重新排列这些数字 %d :", numOne);

关于c - 为什么声明变量后我得到不同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117310/

相关文章:

c++ - 如何搭建应用层预取系统

java - 如何在 C 编程中打印结构,类似于 c 中 Java 的 toString() 方法?

c - System() 参数 C

c - 如何使用 sscanf 在回车符上拆分字符串?

c - timer_create 和timer_settime 生成段错误

c - 优化 C 循环

c - 定义具有多个步骤的函数宏

c - MISRA 错误 10.1 复数整数的隐式转换

与 C 中的指针和虚拟内存混淆

c - C中的符号常量和宏有什么区别?