gets is a more convenient method of reading a string of text containing whitespaces.
Unlike scanf(), it does not skip whitespaces.
It is used to read the input until it encounters newline.
%[^\n]
It is an edit conversion code.
The edit conversion code %[^\n] can be used as an alternative of gets.
C supports this format specification with scanf() function.
This edit conversion code can be used to read a line containing characters like variables and even whitespaces.
In general scanf() function with format specification like %s and specification with the field width in the form of %ws can read-only strings till non-whitespace part.
It means they cannot be used for reading a text containing more than one word, especially with Whitespaces.
Table of difference and similarities between gets() and %[^\n]
gets()
%[^\n]
gets() is used to read strings
%[^\n] is an edit conversion code used to read strings
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article’s discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please Login to comment...