Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - scanf()

C Library Function - scanf()

C Standard Library - <stdio.h> C Standard Library -

Description

C library function *int scanf(const char format, ...) Reads formatted input from the standard input stdin.

Declaration

The following is the declaration of the scanf() function.



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

Parameters

The format specifier has the form:

[=%[*][width][modifiers]type=]

The details are explained below:

Parameters Description
* This is an optional asterisk indicating that data is read from the stream stream but is ignored, that is, it is not stored in the corresponding argument.
width This specifies the maximum number of characters to be read in the current operation.
modifiers : h: short int (for d, i, and n), or unsigned short int (for o, u, and x); l: long int (for d, i, and n), or unsigned long int (for o, u, and x), or double (for e, f, and g); L: long double (for e, f, and g).
type A character that specifies the type of data to be read and how it is read. See the next table for details.

scanf type specifiers:

Type Valid input Argument type
%a, %A Reads 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, passing them via the argument, stores them in consecutive positions of 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 number: includes a decimal point, an optional leading + or - sign, an optional trailing e or E character, and a decimal number. Two valid examples are -732.103 and 7.12e4. float *
%i Reads a decimal, octal, or hexadecimal integer. int *
%o Octal integer. int *
%s String. This reads consecutive characters until a whitespace character is encountered (a whitespace character can be a space, newline, or tab). char *
%u Unsigned decimal integer. unsigned int *
%x, %X Hexadecimal integer. int *
%p Reads a pointer.  
%[] Scans a set of characters.  
%% Reads the % symbol.  

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, EOF is returned.

Example

The following example demonstrates the use 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's compile and run the above program, which will produce the following result in interactive mode:



请输入三个数字:1 2 3

1,2,3

Explanation:

If you use

,

to separate the input

%d

, so the corresponding input must also include

,

:

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's compile and run the above program, which will produce the following result in interactive mode:



请输入三个数字:1, 2, 3

1, 2, 3

**Note:** When entering, the comma must immediately follow the number; there must be no space between the number and the comma.

When inputting with %c, spaces and "escape characters" are 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's 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 reading a string:

Example

#include

<

stdio.h

int

main

(

)

{

char

str1

[

20

]

,

str2

[

30

]

;

printf

(

"

Please enter your username:

"

)

;

scanf

(

"

%s

"

,

str1

)

;

printf

(

"

Please enter your website:

"

)

;

scanf

(

"

%s

"

,

str2

)

;

printf

(

"

Entered username: %s

\

n

"

,

str1

)

;

printf

(

"

Entered website: %s

"

,

str2

)

;

return

(

0

)

;

}

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



请输入用户名:admin

请输入您的网站:www.runoob.com

输入的用户名:admin

输入的网站:www.runoob.com

C Standard Library - <stdio.h> C Standard Library -