C Language-scanf function

Source: Internet
Author: User

1) scanf function, is a standard input function

is a blocking function: When using scanf, the program waits for user input, and if you do not enter the content, the program no longer executes

Function: Receive the content that is lost from the keyboard

2) Use the format:

Compare printf

printf ("Format control string", variable list);

scanf ("Format control string", "Address" list of variables)

The address of the variable: the first address of the variable

method to get the first address of a variable: & variable Name

printf ("A =%d\n", a);

scanf ("%d", &a);

3) Format Control

%d receives an integer

%f Receive real numbers

%c receives characters

...

4) The use of scanf function attention point

(1) When using the SCANF function to receive an "integer", enter a space before entering the Data tab return

The scanf function ignores

(2) scanf function when entering data, use carriage return as Terminator

(3) But when you enter a character, it's a bit of a problem.

Example 1

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

If you enter in the control area: 4 5 7-"The space will be ignored, printed or 4,5,7

Example 2

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

If you enter:4c7-> in the control area, print is 4,c,7

However, if you appear in front of the character < space >< comma >< carriage return, the structure is wrong,

If you enter in the control area: 4 C7->&a will take away 4,&b will take away < space >,&c will take C, and then print it in ASCII code

This is an easy mistake to make (because < spaces > and < Enter >< comma > are characters)

The buffer is where the input is waiting,

C Language-scanf function

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.