23

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?

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Nick
  • 10,309
  • 21
  • 97
  • 201

7 Answers7

30

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;
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Just finding this and learning myself. Have a follow up if anyone reads this....the & is giving you access to the stored location so that you can change the value? That's the way I understand it anyway.... – Dan Dec 18 '18 at 15:24
  • @Dan Yes, you're right. Although you're completely right, if it's still a bit scary to you I recommend you to watch "Ashley Mills" C video tutorials on YouTube on pointers. – aderchox Jan 04 '19 at 07:46
12

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.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
7

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
5

Because otherwise it would only be altering a copy rather than the original.

Corbin
  • 33,060
  • 6
  • 68
  • 78
3

scanf() stores values, so it needs a place to store them.
This is done by providing the addresses (in pointers) of where to store the values using addressof or &(ampersand) operator.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
2

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, you wouldn't be able to directly alter the variable within scanf, so you couldnt set it to the value read in by scanf.

Hope this helps.

Ethan
  • 1,206
  • 3
  • 21
  • 39
0

sscanf does not require &

int decimal;
int *pointer = &decimal;
scanf("%d", pointer);

above code is valid

jacekmigacz
  • 789
  • 12
  • 22