Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does scanf require &?

Tags:

c

scanf

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?

like image 212
Nick Avatar asked May 15 '12 09:05

Nick


People also ask

Why does scanf need address?

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.

Why is it necessary to pass in and not in itself to scanf?

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.

Why is used in scanf () and not in printf?

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

What is the syntax of 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.

Why does scanf require the ADDRESSOF operator (&)?

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?

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.

How to use scanf for learning purposes?

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


3 Answers

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;
}
like image 96
ThiefMaster Avatar answered Oct 08 '22 00:10

ThiefMaster


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.

like image 33
Steve Jessop Avatar answered Oct 07 '22 23:10

Steve Jessop


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

like image 38
Jerry Coffin Avatar answered Oct 08 '22 00:10

Jerry Coffin