[C] scanf-format input function

Source: Internet
Author: User

The scanf function is called the format input function, that is, the content input by the user's keyboard is saved to the specified variable.

 

Scanf function Signature
Scanf ("format control string", variable address list)

 

Scanf is a standard library function. Its prototype is located in <stdio. h>. Similar to the printf function, the C language allows implicit use of the scanf function, that is, the header file <stido. h> can be included in advance. If the scanf or printf function is implicitly used, the system will issue an implicit call warning during compilation.

Scanf cannot display a non-formatted placeholder string on the console, that is, it cannot display an input prompt on the console.

The first parameter "format control string" of scanf can be divided into two parts: one is the format placeholder and the other is the variable separator.

 

Format placeholder
% [*] [Input data width] [length] Type

 

Scanf supports the following input types:

1. d-decimal integer

2. O-octal integers

3. X-hexadecimal integer

4. U-Unsigned decimal integer

5. f or e-decimal form or exponential form of real number

6. C-Single Character

7. S-string

 

Variable Separator

A variable separator is a non-format placeholder used to separate input data when multiple variables are input. For example:

Scanf ("% da % d", & A, & B) on the console, enter:
Result 123: A = 456, B =

  

A In the first parameter of scanf is a variable separator. If the variable separator is not specified in the first parameter of scanf, the system uses the space character, Tab character, or carriage return character as the Separator by default. When the C compiler encounters these three or invalid characters, the input data is deemed to end. For example:

Scanf ("% d", & A, & B); In the console, enter 123 456 or 123456. The result is a = 123, B = 456.

 

Scanf function Example 1. General Usage
int main(void) {    int a, b;    printf("Input a, b\n");    scanf("%d%d", &a, &b);    printf("a=%d, b=%d\n", a, b);    return 0;  }

 

Input and Output:

$ ./a.out Input a, b123 456a=123, b=456

 

2. Use the * symbol to skip the input value.

Modify the code of the previous example:

scanf("%d%*d%d", &a, &b)

 

Input and Output: Skip the second input value 456 and assign the third input value 789 to variable B.

$ ./a.out Input a, b123 456 789a=123, b=789

 

3. Specify the data width in the format placeholder.

Modify the code of the previous example:

scanf("%4d%4d", &a, &b);

 

Input and Output:

$ ./a.out Input a, b12345 56789a=1234, b=5$ ./a.out Input a, b123456789a=1234, b=5678

 

During the first input, the C program directly intercepts the first four digits of 12345 and assigns them to variable A. Then, from the remaining five 56789, it is prepared to take four digits and assign them to variable B, however, a space character is encountered at the second position. As mentioned previously, C program uses the default space character as the Data Separator. Therefore, C program regards that the input of the variable data is complete and assigns 5 to B.

  

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.