2
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

0

1 Answer 1

3

[ 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.