Reputation: 39
So I declared a variable as an integer and initialized it to 0. When I tried printing the value of that variable, it gave me an incredibly high number. int
x=0;
printf("%d", &x);
This is what I did. Am I doing anything wrong? Thanks in advance.
Upvotes: 1
Views: 45
Reputation: 355
The operator '&' represent the address of that variable. we need, the actual value of that variable use like this...
printf("%d",x);
Upvotes: 3
Reputation: 681
You print the address of the x so it will print the address during the compile time it will display the warning
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
So use following
printf("%d",x);
Upvotes: 2
Reputation: 1279
When we are using the &x
, it will refer the address of the x . we need to print the value of x then use this,
printf("%d", x);
In scanf() function, only we need to use &x
, to locate the memory address to store the value.
Upvotes: 2