Scan set in c

Monday, December 26, 2016 Unknown 0 Comments

scanf family functions support scanset specifiers which is denoted by %[]. Inside scanset, we can specify single character or range of characters. Scanset read only those characters which are specified in square bracket. Scansets are case sensitive.

example

#include<stdio.h>
int main(){
char str[100];
printf("Input String:\n");
scanf("%[a-z]s\n",str);   //scan set is case sensitive
printf("you have entered %s\n",str);
return 0;
}
output
Input string:
himanshu123
you have entered
himanshu

if scanset finds ^, it stops reading input.
eg. if ^i specified inside square bracket, scanset will stop reading when it finds first occurrence of i.

example

#include<stdio.h>
int main(){

char str[100];
printf("Input String:\n");
scanf("%[^i]s\n",str);   //scan set is case sensitive
printf("you have entered %s\n",str);
return 0;
}
output:
Input string:
himanshu
you have entered
h

0 comments: