Scans formatted text from stdin
.
Include
<stdio.h>
Prototype
int scanf(const char *format, ...);
Arguments
format
...
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.
Remarks
Each format specifier begins with a percent sign followed by optional fields and a required type as shown here:
%[*][width][modifier]type
*
Indicates assignment suppression. This will cause the input field to be skipped and no assignment made.
width
Specify the maximum number of input characters to match for the conversion, not including white space that can be skipped.
modifier
h modifier | Used with type d, i, o, u, x, X;
converts the value to a short int or unsigned
short int . |
h modifier | Used with n; specifies that the
pointer points to a short int . |
l modifier | Used with type d, i, o, u, x, X;
converts the value to a long int or unsigned
long int . |
l modifier | Used with n; specifies that the
pointer points to a long int . |
l modifier | Used with c; specifies a wide character. |
l modifier | Used with type e, E, f, F, g, G;
converts the value to a double . |
ll modifier | Used with type d, i, o, u, x, X;
converts the value to a long long int or
unsigned long long int . |
ll modifier | Used with n; specifies that the
pointer points to a long long int . |
L modifier | Used with e, E, f, g, G; converts
the value to a long double . |
type
d, i | signed
int . |
o | unsigned int in
octal. |
u | unsigned int in
decimal. |
x | unsigned int in
lowercase hexadecimal. |
X | unsigned int in
uppercase hexadecimal. |
e, E | double in
scientific notation. |
f | double decimal
notation. |
g, G | double (takes the
form of e, E or f as appropriate). |
c | char - a single
character. |
s | string . |
p | value of a pointer. |
n | The associated argument shall be an integer pointer into which is placed the number of characters written so far. No characters are printed. |
% | A % character is
printed. |
Example
For MPLAB X Simulator:
#include <stdio.h>
#include <libpic30.h>
int main(void)
{
int number, items;
char letter;
char color[30], string[30];
float salary;
__attach_input_file("UartIn.txt");
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);
}
// If not using the simulator, remove these lines:
// #include <libpic30.h>
// __attach_input_file("uart_in.txt");
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