目前我正在用 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);
&a
是a
的地址(而且是int*
类型,所以%d
是格式错误)。 a
为您提供 a
的值。
你仍然需要&
在scanf("%d ", &sTime);
; scanf
需要 sTime
的地址,所以它知道在哪里存储值。
关于C - 整数抛出随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141865/