
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Scansets in C
Let us see, what is the scanset in C. The scanset is basically a specifier supported by scanf family functions. It is represented by %[]. Inside scanset we can specify only one character or a set of characters (Case Sensitive). When the scanset is processed, the scanf() can process only those characters which are mentioned in the scanset.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[A-Z]s", str); printf("Given String: %s", str); }
Output
Enter something: HElloWorld Given String: HE
It ignores the characters which are written in lowercase letters. The ‘W’ is also ignored because there are some lower case letters before it.
Now, if the scanset has ‘^’ in its first position, the specifier will stop reading after first occurrence of that character.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^r]s", str); printf("Given String: %s", str); }
Output
Enter something: HelloWorld Given String: HelloWo
Here the scanf() ignores the characters after getting the letter ‘r’. Using this feature, we can solve the problem that a scanf does not take strings with spaces. If we put %[^
], then it will take all of the characters until it gets a new line character.
Example
#include<stdio.h> int main() { char str[50]; printf("Enter something: "); scanf("%[^
]s", str); printf("Given String: %s", str); }
Output
Enter something: Hello World. This line has some spaces. Given String: Hello World. This line has some spaces.
- Related Articles
- isless() in C/C++
- islessgreater() in C/C++
- isgreater() in C/C++
- modf() in C/C++
- isblank() in C/C++
- islessequal() in C/C++
- strxfrm() in C/C++
- Comments in C/C++
- isgreaterequal() in C/C++
- ungetc() in C/C++
- (limits.h) in C/C++
- Pointers in C/C++
- fseek() in C/C++
- strcpy() in C/C++
- strcmp() in C/C++
