scanf function of C language note

Source: Internet
Author: User

Direct excerpt "C Primer Plus" calculation, too classic, I steal lazy, as a review ~

I. Overview
Input from the keyboard is text, because those keys generate text characters: letters, numbers, and punctuation. For example, when you want to enter an integer 2004, you type the characters 2, 0, 0, and 4, and if you want to store them as 4 values instead of strings, your program must convert the string to a numeric value by character, which is what scanf () does! It converts the input string into various forms: integers, Floating-point numbers, characters, and C strings. It is the inverse of printf (), which converts integers, floating-point numbers, characters, and C strings into text to be displayed on the screen.
Like printf (), scanf () uses a control string and a parameter list. The control string indicates the format in which the input will be converted. The main difference is in the parameter list. The printf () function uses variable names, constants, and expressions, while the scanf () function uses pointers to variables. Fortunately, to use this function, there is no need to know anything about pointers, just remember these simple rules:
• If you use scanf () to read the values of one of the basic variable types discussed earlier, precede the variable name with a &.
• If you use scanf () to read a string into a character array, do not use &.

The scanf () function uses spaces (line breaks, tabs, and spaces) to determine how to divide the input into several fields. It matches the conversion specifier to the field in turn and skips the spaces between them. To type at least one line break, space, or tab between each input item. The only exception is the%c description, even if the next character is a white space character, the character will be read, so use%c with extreme caution, I have made such a mistake, debugging for a long time did not understand where the wrong.

The conversion description characters used by the scanf () function are almost identical to those used by printf (). The main difference is that printf () uses%f,%e,%e,%g, and%0 for both float and double types, whereas scanf () only uses them for float types, while the L modifier is required for double types.

Second, the mechanism
We look more closely at how scanf () reads input.
Assuming a%d specifier is used to read an integer, the scanf () function starts reading one input character at a time, skipping white-space characters (spaces, tabs, and line breaks) until a non-whitespace character is encountered. Because it tries to read an integer, scanf () expects to find a numeric character or a symbol (+ or-). If it finds a number or a symbol, it saves it and reads the next character, and if the next character is a number, it holds the number and reads the next character. In this way, scanf () continues to read and save characters until it encounters a non-numeric character. If a non-numeric character is encountered, it concludes that it has read the tail of the integer. scanf () Put this non-numeric character back into the input. This means that the next time the program starts reading the input, it starts with the non-numeric character that was discarded earlier. Finally, scanf () calculates the corresponding numeric value of the number it reads, and places the value in the specified variable.

If you use the field width, scanf () terminates at the end of the field or in the first blank Word prompt (the first one that arrives).

What happens if the first non-whitespace character is not a number? for example, a rather than a number? Then scanf () will stop there and put a (or whatever) back into the input. Without assigning any value to the specified variable, the next time the program reads the input, it restarts at a. If there is only%d specifier in the program, scanf () will never cross that A (to read the next one). Also, if you use the scanf () language with multiple specifiers. Ansic requires the function to stop reading input where the first error occurs.

Reading the input with a different numeric specifier is the same as using%d. The main difference is that scanf () may think of more characters as part of a number. For example, the%x specifier requires scanf () to recognize hexadecimal digits A through f and a through F. The floating-point specifier requires scanf () to recognize the decimal point, exponential notation (e-notation), and the new P notation (p-notation).

If you use the%s specifier, all characters other than white space characters are acceptable, so scanf () skips whitespace characters until it encounters the first non-whitespace character, and then saves any non-whitespace characters before encountering the whitespace character again. This means that scanf () reads a word, that is, a string that does not contain whitespace characters. If you use the field width, scanf () stops at the end of the field or at the first whitespace character. The field width cannot be used to make scanf () read more than one word input with a%s descriptor. The last point: when scanf () puts the string in a specified array, it adds the terminating ' \ \ ' to make the array contents a C string.

If you use the%c specifier, all the input characters are equal. If the next input character is a space or line break, the space or line feed will be assigned to the specified variable: No white-space characters will be skipped.

Third, regular characters in the format string
The scanf () function allows you to place ordinary characters in a format string. Normal characters other than the space character must match the input string exactly.

    1. For example, if you inadvertently place a comma between two specifiers:
      scanf ("%d,%d", &m,%n),
      The scanf () function interprets it as you type a number, type a comma, and then type a number. In other words, you must enter two integers as follows
      88,121
      Because the comma immediately follows%d in the format string, you must enter a comma immediately after 88. However, because scanf () skips whitespace characters before integers, you can type a space or line break after the comma as you type. In other words, the following two input methods can also be accepted:
      88, 121
      Or
      88,
      121

    2. A space in a format string means skipping any spaces before the next entry. For example, the following statement:
      scanf ("%d,%d", &n,&m),
      Any one of the following input lines will be accepted:
      88,121
      88, 121
      88, 121
      Note that the concept of "any space" includes special cases where there are no spaces.
      The scanf ("%d%d", &n,&m) behaves the same as scanf ("%d%d", &n,&m) except that the specifier outside the%c automatically skips the space before the entry.

    3. For%c, adding a space to the format string will cause some differences. For example, if there is a space before%c in the format string, then scanf () jumps to the first non-whitespace character. That is, the command scanf ("%c", &ch) reads the first character encountered in the input, while scanf ("%c", &ch) reads the first non-whitespace character encountered.

Iv. return value of the scanf () function
The scanf () function returns the number of items that were successfully read in. If it does not read any items (this happens when it expects a number and you type a non-numeric string), scanf () returns the value 0. When it detects "end of file", it asks for EOF (EOF is a special value defined in the file stdio.h. The general # define directive defines the value of EOF as-1).

scanf function of C language note

Related Article

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.