[Switch] scanf

Source: Internet
Author: User

The scanf function is called a format input function, that is, the data is input to the specified variable from the keyboard in the format specified by the user.

Common form of scanf Functions

Scanf is a standard library function. Its function prototype is in the header file "stdio. H. Similar to the printf function, the C language also allows stdio. H files not to be included before using the scanf function. The common form of scanf functions is:
Scanf ("format control string", address table column );
The format control string serves the same purpose as the printf function, but cannot display non-format strings, that is, it cannot display the prompt string. The address column lists the addresses of each variable. The address is composed of the address operator "&" and the variable name.

For example, & A and & B indicate the addresses of variable A and variable B respectively.

This address is the address allocated by the compilation system to a and B variables in the memory. In C language, address is used, which is different from other languages. The two concepts of variable value and variable address should be different. The variable address is allocated by the C compilation system. You do not have to worry about the specific address.

Relationship between variable addresses and variable values
Assign values to variables in the value assignment expression, for example:
A = 567;
Then, a is the name of the variable, 567 is the value of the variable, and & A is the address of variable.

But on the left side of the value assignment number is the variable name, and the address cannot be written. The scanf function also assigns a value to the variable in essence, but the variable address must be written, such as &. The two are different in form. & Is a get address operator, and & A is an expression. Its function is to find the address of a variable.

[Example 4-7]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Int A, B, C;
  4. Printf ("input a, B, c \ n ");
  5. Scanf ("% d", & A, & B, & C );
  6. Printf ("A = % d, B = % d, c = % d", A, B, C );
  7. Return 0;
  8. }

In this example, because the scanf function itself cannot display a prompt string, you must first use the printf statement to output a prompt on the screen. Please enter the values of A, B, and C. Run the scanf statement and wait for user input. Because there is no non-format character in the format string of the scanf statement, the interval between "% d" and "% d" is, therefore, more than one space or enter key is used as the interval between each two inputs. For example:
7 8 9
Or
7
8
9

Format String

The format string is generally in the following format:
% [*] [Input data width] [length] Type
The item with square brackets [] is any option. The meaning of each item is as follows.

1) Type
Indicates the type of the input data. The format characters and meanings are shown in the following table.

Format Character meaning
D Input a decimal integer
O Enter an octal integer
X Enter a hexadecimal integer
U Enter an unsigned decimal integer
F or E Input real number (in decimal or exponential form)
C Enter a single character
S Input string


2) "*"
Indicates the input item. If the input item is read, the input value is skipped. For example:

  1. Scanf ("% d % * D % d", & A, & B );

When the input is: 1 2 3, 1 is assigned to a, 2 is skipped, and 3 is assigned to B.

3) Width
Specify the input width (number of characters) with a decimal integer ). For example:

  1. Scanf ("% 5d", & );

Input 12345678 to assign only 12345 to variable A, and the rest is truncated. Another example:

  1. Scanf ("% 4D % 4D", & A, & B );

Input 12345678 will assign 1234 to A and 5678 to B.

4) Length
The length format is L and H, and l indicates the input of long integer data (such as % LD) and Double Precision Floating Point Number (such as % lf ). H indicates the input of short integer data.

Note the following when using the scanf function:

  1. The scanf function does not have precision control. For example, scanf ("% 5.2f", & A); is invalid. You cannot use this statement to enter a real number with two decimal places.
  2. The variables must be given in scanf. If the variable name is given, an error occurs. For example, scanf ("% d", a); is invalid. It should be changed to scnaf ("% d", &.
  3. When multiple numeric values are input, if there are no non-formatted characters in the format control string for the interval between the input data, spaces can be used, tab or press enter for the interval. When C is compiled with spaces, tabs, carriage return, or illegal data (for example, when "% d" is input, "12a" is invalid data), the data ends.
  4. When inputting character data, if the format control string contains no more than the format character, it is considered that all input characters are valid characters.


For example:

  1. Scanf ("% C", & A, & B, & C );

Input D, E, and F to assign 'D' to A, ''to B, and 'e' to C. Only when the input is def can 'D' be assigned to a, 'E' to B, and 'F' to C.

If spaces are added to the format control, for example:

  1. Scanf ("% C", & A, & B, & C );

A space can be added between the input data.

[Example 4-8]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Char A, B;
  4. Printf ("input character a, B \ n ");
  5. Scanf ("% C", & A, & B );
  6. Printf ("% C \ n", a, B );
  7. Return 0;
  8. }

Because the scanf function "% C" does not contain spaces, input m n and the output result is only M. When the input is changed to Mn, two characters of Mn can be output.

[Example 4-9]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Char A, B;
  4. Printf ("input character a, B \ n ");
  5. Scanf ("% C", & A, & B );
  6. Printf ("\ n % C \ n", a, B );
  7. Return 0;
  8. }

In this example, when there is a space between the scanf format control string "% C", there can be a space gap between the input data.

5) if the format control string contains a non-formatted character, you must also enter this non-formatted character.
For example:

  1. Scanf ("% d, % d, % d", & A, & B, & C );

The unformatted separator "," is used as the delimiter. Therefore, the input value must be 5, 6, or 7. Another example:

  1. Scanf ("A = % d, B = % d, c = % d", & A, & B, & C );

The input value is a = 5, B = 6, c = 7.

6) if the input data is different from the output data type, the result is incorrect even though the compilation is successful.

[Example 4-10]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Int;
  4. Printf ("input a number \ n ");
  5. Scanf ("% d", & );
  6. Printf ("% lD", );
  7. Return 0;
  8. }

The output result is inconsistent with the input data because the input data type is integer and the format string of the output statement is long. If the program is changed as follows (Example 4-11 ]):

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Long;
  4. Printf ("input a long integer \ n ");
  5. Scanf ("% lD", & );
  6. Printf ("% lD", );
  7. Return 0;
  8. }

The running result is:
Input a long integer
1234567890
1234567890

When the input data is changed to a long integer, the input and output data is equal.

[Example 4-12]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Char A, B, C;
  4. Printf ("input character a, B, c \ n ");
  5. Scanf ("% C", & A, & B, & C );
  6. Printf ("% d, % d, % d \ n % C, % C, % C \ n", a, B, c, a-32, b-32, c-32 );
  7. Return 0;
  8. }

Enter three lower-case letters and the ASCII code and corresponding upper-case letters.

[Example 4-13]

  1. # Include <stdio. h>
  2. Int main (void ){
  3. Int;
  4. Long B;
  5. Float F;
  6. Double D;
  7. Char C;
  8. Printf ("\ nint: % d \ nlong: % d \ nfloat: % d \ ndouble: % d \ nchar: % d \ n", sizeof (), sizeof (B), sizeof (F), sizeof (D), sizeof (c ));
  9. Return 0;
  10. }

The length of each data type.

[Switch] scanf

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.