April 19, 2018

When scanf() Function Stop Scanning


   The scanf() function scans each input field, character by character. It might stop scanning a particular input field before reaching the normal field-end character (whitespace), or it might terminate entirely.


In this tutorial we will examine situations where the scanf() function either:

  • Stops scanning a particular input field and skips to the next field
  • Stops scanning entirely, and returns

scanf() Stops and Skips to the Next Input Field
 
The scanf() function stops scanning and storing the current input field and proceed to the next one if any of the following occurs:

  • An assignment-suppression character (*) appears after the % in the format specifier. The current input field is scanned but not stored.
  • The number of characters specified by the width specifier have been read.
  • The next character read can't be converted under the current format (for example, the character B is entered when the format is %f).
  • The next character in the input field does not appear in the search set (or does appear in an inverted search set).

When scanf() stops scanning the current input field for one of these reasons, it assumes that the next character is unread and is either

  • the first character of the following input field, or
  • the first character in a subsequent read operation on the input.

scanf() Stops Scanning Entirely, and Returns

The scanf() function will terminate under the following circumstances:

  • The next character in the input field conflicts with a corresponding non-whitespace character in the format string.
  • The next character in the input field is EOF.
  • The format string has been exhausted.

If a character sequence that is not part of a format specifier occurs in the format string, it must match the current sequence of characters in the input field. scanf() function will scan but not store the matched characters. When a conflicting character occurs, it remains in the input field as if the scanf() function never read it.

Assignment-suppression Character

If we want to skip any input field, we can specify * between the % sign and the conversion specification. The input field is read but its value is not assigned to any address. This character * is called the assignment-suppression character. For example:

scanf("%d%*d%d",&a,&b,&c);

Input : 12 23 32

Here 12 is stored in a, 23 is skipped and 32 is stored in b. Since no data is available for c, it has garbage value.


scanf("%d%*c%d%*c%d",&d,&m,&y);

Input : 2/1/2018

Here 2 will be stored in d, then / will be skipped, 1 will be stored in m, again / will be skipped and finally 2018 will be stored in y.

#include <stdio.h>

int main(void)
{
    int a,b,c;
   
    printf("\n\tEnter three numbers : ");
    scanf("%d%*d%d",&a,&b,&c);
    printf("\n\tValue of a : %d",a);
    printf("\n\tValue of b : %d",b);
    printf("\n\tValue of c : %d",c);
   
    return 0;
}


Output :

Enter three numbers : 12 23 32

Value of a : 12
Value of b : 32
Value of c : -858993460


The variable c has garbage value.

Search Set

A search set defines a set of characters enclosed in square braces, [ and ]. These search sets are part of format specifier.

Syntax for search set conversion:
%[search_set]

The search_set specified above is a sequence of characters. Square braces surround a set of characters that define a search set of possible characters making up the string (the input field).


The rules to write search sets are as follows:

Rule 1: The possible set of characters making up the search set is enclosed within square brackets, e.g. [abc]. The scanf() function reads all the characters up to but not including the one that does not appear in a search set.


Example 1: 

%[abcde]   -  Searches the input field for any of the characters a, b, c, d and e. The scanf() function reads the input characters and stops when a character except a, b, c, d or e is encountered.


#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[abcde]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : derived

You entered : de


Please note that the search sets are case-sensitive. If the specified search set is [abcde] and the entered string is Derived, no character will be read, as the character D does not belong to the search set.

Example 2: 

%[0123456789]   -  To scan all decimal digits. 

#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[0123456789]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : 321AXD029

You entered : 321


Rule 2: If the first character in the brackets is a caret (^), the search set is inverted to include all characters except those between the brackets (Normally, a caret will be included in the inverted search set unless explicitly listed somewhere after the first caret).

Example 1: 

%[^K] - Searches the input field for any characters except K. The scanf() function reads the input characters and stops when the character K is encountered.


#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[^K]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : My name is Manish Kumar

You entered : My name is Manish


Example 2:

%[^^] - Searches the input field for any characters except ^.

#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[^^]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : My name is Manish^Kumar

You entered : My name is Manish


Example 3:

%[^\n] - Searches the input field for any characters except newline character ('\n').

#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[^\n]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : Just Coding

You entered : Just Coding


Rule 3: You can also use a range facility shortcut [<first>-<last>] to define a range of letters or numerals in the search set.

Examples :

To identify all decimal digits, you can define the search set in the following way:

(i)  %[0-9]    -   To scan all decimal digits using range

To identify alphanumeric characters, you can use the following syntaxes:
    
(ii) %[A-Z]             -  To scan all uppercase letters

(iii) %[0-9A-Za-z]  -  To scan all decimal digits and all letters

(iv) %[A-FT-Z]       -  To scan all uppercase letters from A through F and from T through Z.


#include <stdio.h>

int main(void)
{
    char str[20];
   
    printf("\n\tEnter a string : ");
    scanf("%[A-Z]",str);
    printf("\n\tYou entered: %s",str);
 
    return 0;
}


Output :

Enter a string : JUST_Coding

You entered : JUST



Rules covering search set ranges:

  • The character prior to the hyphen (-) must be lexically less than the one after it, i.e., inside the search set, a-z is valid, but not z-a.
  • The hyphen must not be the first or last character in the set. If it is first or last, it is considered to just be the hyphen character, not a range definer.
  • The characters on either side of the hyphen must be the ends of the range and not part of some other range.


next    C Operators and Expressions
top    Index
prev    Formatted Input and Output in C 




No comments:

Post a Comment