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
  • 379
  • 5
  • 8
  • 14

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
  • 37,819
  • 6
  • 48
  • 88
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,049
  • 7
  • 31
  • 69
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,303
  • 4
  • 25
  • 42
  • 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