2

I am trying to scan in 1-3 words from the user into a string. However, Only the first word will scan.

3
  • Post the code of what you are doing. Commented Jul 28, 2011 at 3:31
  • Use search facility here. There are number of posts on scanf - stackoverflow.com/questions/tagged/scanf Commented Jul 28, 2011 at 3:38
  • Which company's scanner are you using ? ;) Commented Jul 28, 2011 at 7:29

2 Answers 2

3
scanf("%s", &area ) ;

scanf stops reading from the stream when a space is encountered. You need to use getline instead.

Sign up to request clarification or add additional context in comments.

1 Comment

scanf() is also a huge source of security holes. Not only is getline() more effective, it's also a lot safer.
3

One possibility is to use a scan set conversion instead of a string conversion:

char buffer[256];
scanf("%255[^\n]", buffer);

Much like fgets, this reads up to the end of the line rather than stopping at the first white-space character.

1 Comment

@BillyONeal: Actually, scan-sets are part of standard C (e.g., §7.19.6.2/12 in C99, §7.9.6.2 in C89/90) -- definitely not the best known part, but there nonetheless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.