Learn this soon and learn this well: C does not have a first-class "string" type!
When you wrote
char name[] = "";
you did not declare a string variable, that initially contained an empty string, but that could and would automatically expand to contain any string you tried to assign to it.
No, what you got was an array of char
of size exactly 1, initially containing the empty string, consisting of exactly (and only) the string-terminating character '\0'
. This array can't be used for much of anything: the only thing it's ever going to be able to contain is the empty string, because it doesn't (and will never) have room for anything more.
In C, it is generally your responsibility to know how big your strings are going to be, and to explicitly allocate each variable to refer to enough memory for any string it might contain.
For example, you could write
char name[11] = "";
Now what you're saying is, "Give me an array of characters, sufficient to contain strings up to 10 characters long (plus 1 for the terminating \0
character), initially containing the empty string."
Now you can safely say
scanf("%s", name);
But there are two more points to make.
First, you'll notice that I have left out the &
. You might have gotten the impression that you always need the &
on your variables when you call scanf
. And that's a real rule, but it has an exception: it turns out that you do not need the &
when you're using %s
to read a string into an array. Sometimes the error is innocuous (the code will happen to work anyway), but sometimes it will cause problems (such as when you use %s
to read into an array pointed to by a pointer variable). My compiler warns me warning: format specifies type 'char *' but the argument has type 'char (*)[1]'
when I do something like this.
But second, if we've declared
char name[11] = "";
, then how do we actually enforce that? How do we arrange that we, or a function like scanf
over which we have less control, won't accidentally try to write more than 10 characters to our name
array?
As we've already seen, if you just call
scanf("%s", name);
you don't get any protection. scanf
will read as many characters as the user types, until it sees a newline or other whitespace, and it will write all those characters to the variable you provided, and if that's more characters than the variable can hold, boom, something bad happens.
One way of protecting against this is to give scanf
a limit on the number of characters it can read:
scanf("%10s", name);
Now, by putting that 10
in there, you're telling scanf
not to read a string longer than 10 characters, so it won't overflow your name
array.