A.Aronia A.Aronia - 1 year ago 142
C Question

scanf generates a random number?

Hi I just started C as my first programming language.

I was playing around a bit with

and found something strange.

#include <stdio.h>
#include <Windows.h>
int main()
int x;

printf( " Type any number : " );
scanf( "%d", &x );
printf( "You entered %d.\n", &x );

return 0;

It always shows me a result of 7 or 8 digit number.

How is it possible?

Answer Source

You printf x's address instead of value. Fix like this:

printf("You entered %d.\n", x);

scanf wants the address where to store the result, printf can take the value itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download