0
#include
char option[64],line[256];

main()

{

printf(">>")
(fgets(line, sizeof(line), stdin)) {
            if (1 == sscanf(line, "%s", option)) {
            }
    }
print(option)
}

will only get the first word, for example

/>>hello world

would output

/>>hello

user2341069
  • 389
  • 5
  • 8
  • 14
  • Print `line`, not `option`. – pmg May 04 '13 at 17:07
  • This is expected. From `man scanf`: `%s] - Matches a sequence of non-white-space characters ... The input string stops at white space`. –  May 04 '13 at 17:10
  • Look at this [post](http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – ibi0tux May 04 '13 at 17:11

4 Answers4

0

In sscanf(..., "%s" ...

The scan terminates at whitespace, if you want to print entire line you just have to:

printf("%s", line)

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0
#include <stdio.h>

int main(){
    char option[64],line[256];

    printf(">>");
    if(fgets(line, sizeof(line), stdin)) {
        if (1 == sscanf(line, "%[^\n]%*c", option)) {//[^\n] isn't newline chars
             printf("%s\n", option);
        }
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

You can use a format in scanf that allows you to match whitespaces. Look at @BLUEPIXY good anwser.

Alternatively, you can use getline(). More info here.

ibi0tux
  • 2,481
  • 4
  • 28
  • 49
  • I read somewhere that getline() does not limit the input buffer and is can be overflowed, not sure if my method could but I stayed away from it – user2341069 May 04 '13 at 17:20
0

You can try the following code snippet :

char dump, string[40];
printf("Enter the sentece with spaces:\n");
scanf ("%[^\n]", string);
scanf("%c", &dump);
printf ("You entered: %s", string);

getchar();
getchar();
Fuat Coşkun
  • 1,045
  • 8
  • 18