Normally when we scan int or float or double input we use '&' symbol before the variable but why not in the case of string
-
Because a string decays to pointer when passed as a function argument. – machine_1 Feb 03 '19 at 13:43
-
Have you learned about pointers yet? Even the best of books will only present `scanf` with ampersands and say "trust me until later". – StoryTeller - Unslander Monica Feb 03 '19 at 13:44
-
https://stackoverflow.com/a/18403229/1201599 and https://stackoverflow.com/questions/18403154/when-should-i-use-ampersand-with-scanf/18403229#18403229 – daniyel Feb 03 '19 at 13:45
2 Answers
When scanning a string, the %s
format specifier expects an argument of type char *
. It is used like this:
char str[100];
scanf("%99s", str);
We don't need to use &
here because str
, when used in a expression decays to a pointer to its first element and has type char *
. So there's no need to take the address since you already have a `char *.
If you're using dynamically allocated memory:
char *str = malloc(100);
scanf("%99s", str);
You explicitly have a char *
already, so again no need to take the address. If you did, you'd get the address of the pointer variable instead of the address of the allocated memory. Bad things will happen if you do this.

- 205,898
- 23
- 218
- 273
It is because in this case array decays to the pointer and this pointer referencing the first char element of this array. So no &
is needed
char str[LEN];
scanf("%s", str)
is equivalent to scanf("%s", &str[0])

- 60,014
- 4
- 34
- 74