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

Replies

Oldest  Newest  Rating
0 Linguist Llama · December 23, 2015
Please see the StackOverflow answer I have written here: http://stackoverflow.com/questions/30271631/dynamically-allocate-user-inputted-string/30272281#30272281
0 ileana jackson · December 18, 2015
Thanks everyone
0 c student · November 6, 2015
you use fgets
0 Akshay Narula · November 6, 2015
#include<string.h>


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.
  • 1

C

107,055 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator