Reputation: 49
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?
Upvotes: 1
Views: 297
Reputation: 144740
You pass the address of a
instead of its value to printf
. You should also output a linefeed to separate the numbers:
printf("%d\n", a);
Upvotes: 0
Reputation: 786
When you use
printf("%d", &a);
this means that it will print the address of a
and to print the value of a
you have to wright
printf("%d", a);
and after making the changes compile the program and try to rerun :)
Upvotes: 1
Reputation: 134336
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.
Upvotes: 1