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:

ParameterDescription
*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.
widthThis specifies the maximum number of characters to be read in the current read operation.
modifiersSpecifies 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)
typeA 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:

TypeQualifying inputargument type
%a、%A Reads in a floating-point value (valid only in C99).float *
%cSingle 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 *
%dDecimal integer: the + or - sign before the number is optional.int *
%e、%E、%f、%F、%g、%GFloating 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.12e4float *
%iReads in decimal, octal, and hexadecimal integers.int *
%oOctal integer.int *
%sString. This reads consecutive characters until a whitespace character is encountered (whitespace characters can be spaces, newlines, and tabs).char *
%uAn unsigned decimal integer.unsigned int *
%x、%XHexadecimal 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

#include<stdio.h> int main(void) { int a,b,c; printf("Please enter three numbers:"); scanf("%d%d%d",&a,&b,&c); printf("%d,%d,%d\n",a,b,c); return 0; }

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.
If using,To separate the input%d, and it also needs to be added when entering the corresponding input,:

Example

#include<stdio.h> int main(void) { int a,b,c; printf("Please enter three numbers:"); scanf("%d, %d, %d",&a,&b,&c); printf("%d, %d, %d\n",a,b,c); return 0; }

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

#include<stdio.h> int main(void) { char a,b,c; printf("Please enter three characters:"); scanf("%c%c%c",&a,&b,&c); printf("%c,%c,%c\n", a,b,c); return 0; }

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

#include <stdio.h> int main() { char str1[20], str2[30]; printf("Please enter username:"); scanf("%s", str1); printf("Please enter your website:"); scanf("%s", str2); printf("Input username: %s\n", str1); printf("Entered website: %s", str2); return(0); }

Let us compile and run the above program, which will produce the following result in interactive mode:

请输入用户名:admin
请输入您的网站:www.example.com
输入的用户名:admin
输入的网站:www.example.com

C Standard Library - <stdio.h>

other extensions