3

Possible Duplicate:
How do you allow spaces to be entered using scanf?

char inputStr[64];
int inputInt;
printf("Enter a number:");
scanf("%d",&inputInt);
printf("\nEnter a string:");
scanf("%s",&inputStr);

The above code has 2 part, reading a integer and string.

Reading an integer is fine.

Reading a string also fine considering characters are under 64 and there is no SPACE.

That is the problem.

scanf only reads the character up to a space.

Everything after the space is gone.

How can I include the space also in scanf?

Community
  • 1
  • 1
william
  • 7,284
  • 19
  • 66
  • 106

4 Answers4

3

Rather than using scanf("%s", ...), use:

fgets(inputStr, 64, stdin)

It's better practice as it isn't prone to buffer overflow exploits and it will read in a whole line of input, which it seems you're trying to do. Note that the newline character, \n, is also read into the buffer when using fgets (assuming that the whole line manages to fit into the buffer).

Also, when passing an array to a function, as you do with your second scanf call, you don't need to use the address-of operator (&). An array's name (ie. inputStr) is the address of the start of the array.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
2

Use fgets instead (gets is unsafe) if you want to read a line instead.

fgets(inputStr, 64, stdin)
Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • 1
    +1 Or maybe `scanf("%63[^\n]");` – cnicutar Nov 07 '11 at 06:09
  • @cnicutar While I actually wanted to suggest that it's hard to readd for some people so I haven't actually used it in production code for a while and tend to avoid it. But I would definitely +1 that as an answer :) – Jesus Ramos Nov 07 '11 at 06:14
  • @cnicutar - While that would work, the temptation to combine this and make more complicated `scanf` format specifiers is too great, and error recovery in such cases is too difficult. I'd rather use `fgets` / `sscanf` than plain `scanf` (or `fscanf`) any day. – Chris Lutz Nov 07 '11 at 06:18
  • @ChrisLutz I agree sometimes its easier for readability than saving you an extra line or two of code and makes it easier to deal with that trailing newline and such. – Jesus Ramos Nov 07 '11 at 06:42
1

If you want to continue using scanf (e.g., if this string is only some of the input you're reading with scanf), you could consider using a "scanset" conversion, such as:

scanf("%63[^\n]%*c", inputStr);

Unlike fgets, this does not include the trailing new-line in the string after reading. Also note that with a scanset conversion, you specify the maximum length of string to read rather than the buffer size, so you need to specify one smaller than the buffer size to leave space for the terminating NUL.

I've included the "%*c" to read but discard the new-line. A new-line is considered white-space, which is treated specially in a scanf conversion string. A normal character would just match and ignore that character in the input, but white-space matches and ignores all consecutive white-space. This is particularly annoying with interactive input, because it means the white-space conversion won't finish matching until you enter something other that white-space.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Using `"%*c"` can be bad - if the user types in more than 63 characters, it will discard the last character (thinking it's a newline). You could do `"%*[\n]"`, but you'd still have to use `scanf`'s wonderful (read: horrible) error checking mechanisms to see if it did, in fact, register a newline or if there's more pending user input. – Chris Lutz Nov 07 '11 at 06:27
  • @ChrisLutz: Quite true. Most people just ignore extra like that anyway (or treat it as an error), but if you *want* to (for example) expand your allocation and read more when they enter more, that's easier to hand with scanf than fgets: `scanf(%63[^\n]%c", inputStr, &someChar); if (someChar != '\n') read_more();` – Jerry Coffin Nov 07 '11 at 06:31
0

Jesus has the answer -- switch to fgets(3); in case you're curious why you cannot capture whitespace with scanf(3):

   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 character ('\0'), which is added
          automatically.  The input string stops at white space
          or at the maximum field width, whichever occurs first.
Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238