[英]why won't this program give me the output i want?
I've written this simple code so I can see what's wrong with a more complex program that I have written. 我已经写了这个简单的代码,所以我可以看到我编写的一个更复杂的程序出了什么问题。
#include<stdio.h>
int main()
{
int n = 0, i = 1, a = 0;
scanf("%d", &n);
while (i <= n)
{
scanf(" %d", &a);
printf("%d", &a);
i++;
}
}
but when I run the program it goes like this: 4 1 6487620 what's wrong with it? 但是当我运行程序时,它会变成这样:4 1 6487620它怎么了?
In your code 在你的代码中
printf("%d", &a);
should be 应该
printf("%d", a); // don;t print address....
FWIW, passing an address (a pointer type) as an argument to %d
is a mismatch and invokes undefined behavior . FWIW将地址(指针类型)作为参数传递给
%d
是不匹配的,并调用未定义的行为 。
When you use printf("%d", &a);
当您使用
printf("%d", &a);
this means that it will print the address of a
这意味着它会打印的地址
a
and to print the value of a
you have to wright 并打印的价值
a
你必须赖特
printf("%d", a);
and after making the changes compile the program and try to rerun :) 并进行更改后,编译程序并尝试重新运行:)
You pass the address of a
instead of its value to printf
. 您传递的地址
a
,而不是其价值printf
。 You should also output a linefeed to separate the numbers: 您还应该输出换行符以分隔数字:
printf("%d\n", a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.