A.Aronia A.Aronia - 3 months ago 24
C Question

scanf generates a random number?

Hi I just started C as my first programming language.

I was playing around a bit with

scanf()
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 );

system("pause");
return 0;
}


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

How is it possible?

Answer

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.

Comments