scanf Function

Scans formatted text from stdin.

Include

<stdio.h>

Prototype

int scanf(const char * restrict format, ...);

Arguments

format
format control string
...
optional arguments

Return Value

Returns the number of items successfully converted and assigned. If no items are assigned, a 0 is returned. EOF is returned if an input failure is encountered before the first conversion.

Remarks

When building with the MPLAB XC8 compiler for PIC MCUs, the scanf() function relies on the getch() function being properly defined to read input from the required peripheral or location. Scanning will not work as expected until the getch() stub (found in the pic/sources/c99/common/getch.c of your compiler distribution) is completed. See Example code for 8-bit PIC MCUs for information on defining getch() so that it can read text from a file when you are using the MPLAB X IDE simulator.

To reduce code size, the functionality of this routine can be customized with each build. See the Smart IO Routines section in your compiler's user's guide for more information.

The format string can be composed of white space characters, which reads input up to the first non-white-space character in the input; or ordinary multibyte characters, which if not read in that order, will trigger an error and leave the remaining characters in the input, unread. The format string can also contain conversion specifications, which begin with the % character and that specify the expected input sequences and how they are to be converted for assignment to objects pointed to by the pointer arguments.

Each conversion specification begins with a percent character followed by optional fields and a required type as shown here:

%[*][width][length]specifier

The presence of * immediately after the percent character indicates that the read and converted value should not be assigned to any object.

The width is a decimal integer that must be non-zero and indicates the maximum field width (in characters) of the input that is read to match the specification.

The length modifier specifies the size of the object in which the value will be assigned, as described in the following table. Their use with other conversion specifiers results in undefined behavior.

Modifier Meaning
h  When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a short int or unsigned short int object referenced by the pointer argument.
hh When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a signed char or unsigned char object referenced by the pointer argument.
l When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a long int or unsigned long int object referenced by the pointer argument.
l When used the a, A, e, E, f, F, g, or G indicates that this conversion specifier assigns to a double object referenced by the pointer argument.
l When used the c, s, or [ indicates that this conversion specifier assigns to a wchar_t object referenced by the pointer argument.
ll When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a long long int or unsigned long long int object referenced by the pointer argument.
L When used the a, A, e, E, f, F, g, G conversion specifier, indicates the argument value is a long double.
j When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a intmax_t or uintmax_t object referenced by the pointer argument.
t When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a ptrdiff_t object referenced by the pointer argument.
z When used with d, i, o, u, x, X, or n indicates that this conversion specifier assigns to a size_t object referenced by the pointer argument.

The conversion specifiers specify the type of conversion to be applied to the read sequence.

Specifier Receiving object type Matches
a double Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected by the strtod function for the first argument.
c char array, or wchar_t array Matches a single character or the number of characters specified by the field width if present. If an l length modifier is present, the input shall be a sequence of multibyte characters, converted to a wide character as if by a call to the mbrtowc function. No null character or wide character is added.
d signed int Matches an optionally signed decimal integer, whose format is the same as expected by the strtod function for the first argument when using a value of 10 for the base argument.
e double Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected by the strtod function for the first argument.
f double Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected by the strtod function for the first argument.
g double Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected by the strtod function for the first argument.
i signed int Matches an optionally signed decimal integer, whose format is the same as expected by the strtod function for the first argument when using a value of 0 for the base argument.
n int No input is consumed but the number of characters this call has so far read from the input stream is written to the argument with no increment of the assignment count returned by the function. If the conversion specification includes an assignment-suppressing character or a field width, the behavior is undefined.
o unsigned int Matches an optionally signed decimal integer, whose format is the same as expected by the strtoul function for the first argument when using a value of 8 for the base argument.
p void * * Matches an implementation-defined set of sequences, which should be the same as the set of sequences that may be produced by the %p conversion of the fprintf function. The input item is converted to a pointer value in an implementation-defined manner. If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the %p conversion is undefined.
s char array, or wchar_t array

Matches a sequences of non-white-space characters, which is written to the array argument and appended with a null terminating character. If an l length modifier is present, the input shall be a sequence of multibyte characters, converted to a wide character and terminated with a null wide character.

x unsigned int Matches an optionally signed decimal integer, whose format is the same as expected by the strtoul function for the first argument when using a value of 16 for the base argument.
%   Matches a single % character. No assignment takes place.
[ char array, or wchar_t array Matches all the characters in the input that have been specified between the [ and trailing ] character, unless the character after the opening bracket is a circumflex, ^, in which case a match is only made with characters that do not appear in the brackets. If the conversion specifier begins with [] or [^], the right bracket character will match the input and the next following right bracket character is the matching right bracket that ends the specification. A null character will be appended to the characters read. If an l length modifier is present, the input shall be a sequence of multibyte characters and a null wide character will be appended to the matched wide characters.

Example

#include <stdio.h>

int main(void)
{
  int number, items;
  char letter;
  char color[30], string[30];
  float salary;

  printf("Enter your favorite number, favorite letter, ");
  printf("favorite color desired salary and SSN:\n");
  items = scanf("%d %c %[A-Za-z] %f %s", &number, &letter, &color, &salary, &string);

  printf("Number of items scanned = %d\n", items);
  printf("Favorite number = %d, ", number);
  printf("Favorite letter = %c\n", letter);
  printf("Favorite color = %s, ", color);
  printf("Desired salary = $%.2f\n", salary);
  printf("Social Security Number = %s, ", string);
}

Example Input

Contents of UartIn.txt (used as stdin input for simulator):

"5 T Green 300000 123-45-6789"

Example Output

Enter your favorite number, favorite letter, 
favorite color, desired salary and SSN:
Number of items scanned = 5
Favorite number = 5, Favorite letter = T
Favorite color = Green, Desired salary = $300000.00
Social Security Number = 123-45-6789