printf("%d is what I entered\n", &number);
incorrect because %d (in printf ) expects an argument of type int , not int* . This calls Undefined Behavior , as shown in the draft (n1570) of the C11 standard (highlight):
7.21.6.1 fprintf function
[...]
- If the conversion specification is not valid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined .
Fix it using
printf("%d is what I entered\n", number);
Then Why does scanf require & before the variable name?
Keep in mind that when you use number , you pass the value of the variable number , and when you use &number you pass the address number ( & is the operator address).
So scanf does not need to know the value of number . He needs an address (in this case int* ) to write input to it.
printf , on the other hand, does not require an address. You just need to know the value ( int , in this case) to print. This is why you do not use & in front of a variable name in printf .
Cool guy
source share