Why scanf must take the address of operator

Viewed 10857

As the title says, I always wonder why scanf must take the address of operator (&).

8 Answers

It tells where to write the input value, as the address of (&) operator give the address of the variable. so, scanf with variable name and address operator meane to write the value at this location. You can also check the address of any variable with the address of(&) operator and %p format specifier in the hexadecimal format.

It's because you are storing something. Just think about how a function must work. With printf, you can think of that as a void function that just outputs the result and then it is done. 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. If you took an argument of data type, say, "int", the data would be lost as soon as you exit the scanf function, in other words, in the next line of code in the parent function, that data you scanfed would be gone.

Related