Scansets in C - GeeksforGeeks

06 Jul, 2021
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 square brackets. Please note that the scansets are case-sensitive.
We can also use scanset by providing comma in between the character you want to add.
example: scanf(%s[A-Z,_,a,b,c]s,str);
This will scan all the specified character in the scanset.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.
C
/* A simple scanset example */#include <stdio.h> int main(void){    char str[128];     printf("Enter a string: ");    scanf("%[A-Z]s", str);     printf("You entered: %s\n", str);     return 0;}
  [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 occurrence of that character. For example, given below scanset will read all characters but stops after first occurrence of ‘o’ 
C
scanf("%[^o]s", str);
Let us see with example.
C
/* Another scanset example with ^ */#include <stdio.h> int main(void){    char str[128];     printf("Enter a string: ");    scanf("%[^o]s", str);     printf("You entered: %s\n", str);     return 0;}
  [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.
C
/* implementation of gets() function using scanset */#include <stdio.h> int main(void){    char str[128];     printf("Enter a string with spaces: ");    scanf("%[^\n]s", str);     printf("You entered: %s\n", str);     return 0;}
  [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.
tornado711
nidhi_biet
neeraj844singh
varshagumber28
C-Input and Output Quiz
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
fork() in C
Command line arguments in C/C++
Function Pointer in C
Substring in C++
Different methods to reverse a string in C/C++
Structures in C
TCP Server-Client implementation in C
Enumeration (or enum) in C
std::string class in C++
Data Types in C