Possible Duplicate:
Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)?
In C when we use the scanf()
function to get user input, we always use the &
sign before the variable.
For example:
scanf("%d", &number);
scanf("%c", &letter);
This ensures our input is stored in proper address. But in case of a string we do not use &
.
Why is that?
A "string" in C is the address of a character buffer.
You want scanf
to fill the memory in the buffer, which is pointed to by the variable.
In contrast, an int
is a block of memory, not an address. In order for scanf
to fill that memory, you need to pass its address.
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