Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36601

how this works to accept multi-word string using scanf?

printf("enter the string");
    scanf("%[^\t\n]s",str); 

//how the abv code works to accept multi word string without using gets and only using scanf

Upvotes: 2

Views: 1897

Answers (1)

Fred Foo
Fred Foo

Reputation: 363547

[ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex (^).

...

The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

(Linux scanf(3) manpage.)

So scanf reads up to the first character that is not a tab (\t) or newline (\n). It does read spaces.

Upvotes: 3

Related Questions