I want to read a number from stdin. I don't understand why scanf requires the use of & before the name of my variable:
int i;
scanf("%d", &i);
Why does scanf need the address of the variable?
With scanf you are wanting to RETAIN some data, so you need a pointer aka address where the data you input will be stored even after you leave the function.
When passing stuff to scanf(), you need to pass in a pointer to the variable, not the variable itself. The & means "Don't take the variable, take the place in memory where this variable is stored." It's a little complicated, but it's necessary so that C can change the value of the variable.
Use of & in scanf() but not in printf() As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf().
Below is syntax of Scanf. It requires two arguments: scanf ("Format Specifier", Variable Address); Format Specifier: Type of value to expect while input Variable Address: &variable returns the variable's memory address. In case of a string (character array), the variable itself points to the first element of the array in question.
7 Answers. scanf requires the addressOf operator (&) because it takes a pointer as an argument. Therefore in order to pass in a variable to be set to a passed in value you have to make a pointer out of the variable so that it can be changed. The reason a pointer must be passed to scanf is that if you just passed a variable,...
What does scanf ("%* [^ ] %*c") do? It is a scanf () with scanset and this scanf () function takes all the characters except newline character. How can we use scanset to restrict any character input from user ? How to read a string with spaces using scanf () in C ? Was this worth your time? This helps us sort answers on the page.
But if you have to use scanf for learning purposes, here’s how to use it: You need to pass a valid pointer to it. Not a value. That’s a common mistake most C learners make. So if you have a variable int x; and you want to scan something in it, use it like scanf (“%d”, &x); and not scanf (“%d”, x);
It needs to change the variable. Since all arguments in C are passed by value you need to pass a pointer if you want a function to be able to change a parameter.
Here's a super-simple example showing it:
void nochange(int var) {
// Here, var is a copy of the original number. &var != &value
var = 1337;
}
void change(int *var) {
// Here, var is a pointer to the original number. var == &value
// Writing to `*var` modifies the variable the pointer points to
*var = 1337;
}
int main() {
int value = 42;
nochange(value);
change(&value);
return 0;
}
C function parameters are always "pass-by-value", which means that the function scanf only sees a copy of the current value of whatever you specify as the argument expression.
In this case &i is a pointer value that refers to the variable i. scanf can use this to modify i. If you passed i, then it would only see an uninitialized value, which (a) is UB, (b) is not sufficient information for scanf to know how to modify i.
It's not needed.
char s[1234];
scanf("%s", s);
Works just fine without a single & anywhere. What scanf and company need are pointers. To let it modify a particular variable, you pass the address of that variable. For a few types that happens by default. For others, you use & to take the address (get a pointer to that variable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With