How to accept spaces

+1 ileana jackson · November 6, 2015
when getting user input and storing it into a structure, how do you get the scanf to accept spaces when entering a string?

Post a Reply


Oldest  Newest  Rating
0 c student · November 6, 2015
you use fgets
0 Akshay Narula · November 6, 2015

use scanf("%s", variable);

%s not accept spacebar input as a end mark, unless you hit enter it continues to write a string in the memory with the spaces. 
0 c student · November 6, 2015
@Akshay Narula

from scanf man page using %s:
Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
0 ileana jackson · December 18, 2015
Thanks everyone
0 Linguist Llama · December 23, 2015
Please see the StackOverflow answer I have written here:
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator