SCANF's understanding

Source: Internet
Author: User

Function Name: scanf
Function: Perform formatted input
Usage: int scanf (char *format[,argument,...]);
The scanf () function is a universal terminal format input function that reads input information from a standard input device (keyboard). You can read any intrinsic type of data and automatically transform the values into the appropriate in-machine format.
Its invocation format is: scanf ("< formatted string >",< Address Table >);
The scanf () function returns the number of data items that were successfully assigned, and EOF is returned when an error occurs.
The control string is composed of three types of characters:
1, format specifier;
2, blank character;
3. Non-whitespace character; (A) formatting instructions format character description
%a read in a floating-point value (valid only C99)
%A Ibid.
%c reads in one character
%d read-in decimal integer
%i read in decimal, octal, hexadecimal integer
%o Read in octal integer
%x read in hexadecimal integer
%x Ibid.
%c reads in one character
%s read in a string
%f read in a floating-point number
%F Ibid.
%e Ibid.
%E Ibid.
%g Ibid.
%G Ibid.
%p read in a pointer
%u read in an unsigned decimal integer
%n the equivalent number of characters that have been read into the value
%[] Scan Character Set
Percent Read% sign
Additional format specifier character descriptor
Modifier description
l/l length modifier Enter "Long" data
H-Length modifier input "short" data
W integer constant Specifies the width of the input data
* Asterisk empty read a data
Hh,ll Ibid. h,l but only valid for C99 (B) white space characters
Whitespace characters cause the scanf () function to omit one or more whitespace characters from the input in a read operation, and the white space character can be space,tab,newline and so on until the first non-whitespace character appears.
(C) non-whitespace characters
A non-whitespace character causes the scanf () function to reject characters that are the same as this non-whitespace character when read. The use of the control string for the scanf () function1, when entered with "%c", the Space and "escape character" are valid characters.
Cases:
scanf ("%c%c%c", &AMP;C1,&AMP;C2,&AMP;C3);
Input: A-b-c
Result: A→C1,-→C2,B→C3 (the rest is discarded)

2, the scanf () function receives the input data <%[],%c excepted, when the following situation ends the input of a data: (not the end of the scanf function, the scanf function only has data in each data field, and press ENTER after the end).
① the space, "enter", "Jump" key.
② to the end of the width.
③ in the event of illegal input. So if you want to read a character array with spaces, use the%[] 3, scanf convection operation to follow the type-matching operation principle, and if the type does not match, it will not read the input stream.  So the input stream will be stuck, and if the input stream is not empty, scanf will not wait for user input and enter directly from the buffer. But how does scanf () match?      What is stdin<stdout,stderr> again? For buffer handling, you can call Fflush (stdin) before scanf, and the following procedures are analyzed below: 1#include <stdio.h>

2int Main (int argc, char *argv[])
3{
4 int i;
5 char C;
6 for (i = 0; i < 5; i++)
7 {
8 scanf ("%d", &c);
9 printf ("%d", I);
10}
return 0;
12} Please determine the input 1, 65, 97, 256, 1024, what will the program output? What does the program output, judging by the input a? Explain why? < tip: Consider caching, type matching >

SCANF's understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.