C library functions -scanf()
C Standard Library - <stdio.h>
Description
C Library Functionsint scanf(const char *format, ...)Read formatted input from standard input stdin.
Declaration
The following is the declaration of the scanf() function.
int scanf(const char *format, ...)
Parameter
- format-- This is a C string, containing one or more of the following items:whitespace characters, non-whitespace charactersandformat specifiers。
The format specifier has the form:
[=%[*][width][modifiers]type=]
The specific explanation is as follows:
| Parameter | Description |
|---|---|
| * | This is an optional asterisk, indicating that the data is read from the stream but can be ignored, i.e., it is not stored in the corresponding argument. |
| width | This specifies the maximum number of characters to be read in the current read operation. |
| modifiers | Specifies a size different from integer (for d, i, and n), unsigned integer (for o, u, and x), or floating-point (for e, f, and g) for the data pointed to by the corresponding additional argument: h: short integer (for d, i, and n), or unsigned short integer (for o, u, and x) l: long integer (for d, i, and n), or unsigned long integer (for o, u, and x), or double precision (for e, f, and g) L: long double precision (for e, f, and g) |
| type | A character specifying the type of data to be read and the way the data is read. See the next table for details. |
scanf type specifiers:
| Type | Qualifying input | argument type |
|---|---|---|
| %a、%A | Reads in a floating-point value (valid only in C99). | float * |
| %c | Single character: reads the next character. If a width other than 1 is specified, the function reads width characters and, through the argument, stores them in consecutive positions in the array. No null character is appended at the end. | char * |
| %d | Decimal integer: the + or - sign before the number is optional. | int * |
| %e、%E、%f、%F、%g、%G | Floating point: contains a decimal point, an optional leading sign + or -, an optional trailing character e or E, and a decimal number. Two valid examples are -732.103 and 7.12e4 | float * |
| %i | Reads in decimal, octal, and hexadecimal integers. | int * |
| %o | Octal integer. | int * |
| %s | String. This reads consecutive characters until a whitespace character is encountered (whitespace characters can be spaces, newlines, and tabs). | char * |
| %u | An unsigned decimal integer. | unsigned int * |
| %x、%X | Hexadecimal integer. | int * |
| %p | Reads a pointer. | |
| %[] | Scans a character set. | |
| %% | Read the % sign. |
- Additional arguments-- Depending on the different format string, the function may require a series of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format argument. The number of arguments should be the same as the number of % tags.
Return Value
If successful, the function returns the number of successfully matched and assigned items. If the end of the file is reached or a read error occurs, it returns EOF.
Example
The following example demonstrates the usage of the scanf() function.
Example
Let us compile and run the above program, which will produce the following result in interactive mode:
请输入三个数字:1 2 3 1,2,3
Parsing notes:
- 1、&a、&b、&cin&Is the address operator, which respectively obtains the memory addresses of these three variables.
- 2、%d%d%dIt inputs three numeric values in decimal format. When inputting, one or more spaces, Tab keys, or Enter keys can be used to separate the two data items.
Example
Let us compile and run the above program, which will produce the following result in interactive mode:
请输入三个数字:1, 2, 3 1, 2, 3
Note:When inputting,Must immediately follow the number; the number and,No spaces are allowed between them.
When using %c for input, spaces and "escape characters" are both treated as valid characters.
Example
Let us compile and run the above program, which will produce the following result in interactive mode:
$ ./a.out 请输入三个字符:run r,u,n $ ./a.out 请输入三个字符:r u n r, ,u
The following example demonstrates receiving a string:
Example
Let us compile and run the above program, which will produce the following result in interactive mode:
请输入用户名:admin 请输入您的网站:www.example.com 输入的用户名:admin 输入的网站:www.example.comother extensions