The %s
conversion specifier causes scanf
to stop at the first whitespace character. If you need to be able to read whitespace characters, you will either need to use the %[
conversion specifier, such as
scanf("%[^\n]", bookname);
which will read everything up to the next newline character and store it to bookname
, although to be safe you should specify the maximum length of bookname in the conversion specifier; e.g. if bookname has room for 30 characters counting the null terminator, you should write
scanf("%29[^\n]", bookname);
Otherwise, you could use fgets()
:
fgets(bookname, sizeof bookname, stdin);
I prefer the fgets()
solution, personally.