- 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.
- 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() is used to read strings||%[^\n] is an edit conversion code used to read strings|
|Unlike scanf(), gets() reads strings even with whitespaces||%[^\n] also reads strings with whitespaces|
|when it reads a newline character then the gets() function will be terminated||%[^\n] also terminates with a newline character|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between str.capitalize() VS str.title()
- Difference Between malloc() and calloc() with Examples
- Understanding "volatile" qualifier in C | Set 2 (Examples)
- Pointers in C/C++ with Examples
- Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
- Logical Not ! operator in C with Examples
- iswprint() in C/C++ with Examples
- iswgraph() in C/C++ with Examples
- btowc() function in C/C++ with Examples
- mbrtoc16() in C/C++ with Examples
- mbrtoc32() in C/C++ with Examples
- wmemset() in C/C++ with Examples
- wcsspn() function in C/C++ with Examples
- Types of Literals in C/C++ with Examples
- Difference between an Integer and int in Java with Examples
- cauchy_distribution a() in C++ with Examples
- chdir() in C language with Examples
- Commonly used String functions in C/C++ with Examples
- Methods to concatenate string in C/C++ with Examples
- SDL library in C/C++ with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : abhiramveeramalla