scanf family functions support scanset specifiers which are represented by %. Inside scanset, we can specify single character or range of characters. While processing scanset, scanf will process only those characters which are part of scanset. We can define scanset by putting characters inside squre brackets. Please note that the scansets are case-sensitive.
Let us see with example. Below example will store only capital letters to character array ‘str’, any other character will not be stored inside character array.
[root@centos-6 C]# ./scan-set Enter a string: GEEKs_for_geeks You entered: GEEK
If first character of scanset is ‘^’, then the specifier will stop reading after first occurence of that character. For example, given below scanset will read all characters but stops after first occurence of ‘o’
Let us see with example.
[root@centos-6 C]# ./scan-set Enter a string: http://geeks for geeks You entered: http://geeks f [root@centos-6 C]#
Let us implement gets() function by using scan set. gets() function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF found.
[root@centos-6 C]# ./gets Enter a string with spaces: Geeks For Geeks You entered: Geeks For Geeks [root@centos-6 C]#
As a side note, using gets() may not be a good idea in general. Check below note from Linux man page.
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. Also see this post.
This article is compiled by “Narendra Kangralkar“ and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- C program to store Student records as Structures and Sort them by Name
- Similarities and Differences between Ruby and C language
- Program to copy the contents of one array into another in the reverse order
- Interesting facts about C Language
- Program to Reverse a String using Pointers
- Difference between Structure and Array in C
- Structured Programming Approach with Advantages and Disadvantages
- Commonly used String functions in C/C++ with Examples
- Program to check if two strings are same or not
- putchar() function in C
- Difference between C and C++
- Code Optimization Technique (logical AND and logical OR)
- Difference between C and Python
Improved By : tornado711