The basic course of C language (iii) input and output functions and control flow statements (4)

Source: Internet
Author: User
Tags integer variables printf
Second, scanf () function
The scanf () function is a formatted input function that reads the input information from the standard input device (keyboard). The calling format is:
scanf ("< format string >", < Address table >);
The format string consists of the following three different types of characters;
1. Format specifier: The format specifier is essentially the same as the format specifier in the printf () function.
2. White-space characters: whitespace characters cause the scanf () function to omit one or more whitespace characters from the input in the read operation.
3. Non-white-Space character: a non-white-space character causes the scanf () function to remove the same character from the Non-white-space character as it reads.
The Address table is the address of all variables that need to be read, not the variable itself. This is completely different from the printf () function, with particular attention. The address of each variable is separated from the ",".
Example 2:
Main ()
{
int I, J;
printf ("I, j=?\n");
scanf ("%d,%d", &i, &j);
}
The scanf () function in the previous example reads an integer and then removes the comma from the input and finally reads another integer. If "," this particular character is not found, the scanf () function terminates. If the delimiter between the arguments is a space, one or more spaces must be entered between the parameters.
Description
(1). For string arrays or string pointer variables, the scanf () function does not need to precede them with the "&" operator, because the array name and the pointer variable name are themselves addresses.
Example 3
Mian ()
{
Char *p, str[20];
scanf ("%s", p); /* Input string from health disk * *
scanf ("%s", str);
printf ("%s\n", p); /* To the screen output string * *
printf ("%s\n", str);
}
(2). You can add an integer to the "%" formatting specifier in the formatted string to represent the maximum number of digits in any read operation.
The first scanf () function statement becomes scanf ("%10s", p) if it is specified that only 10 characters can be entered into the string pointer p in example 3;
When the program runs, once the number of characters is greater than 10,p, the reading is no longer read, and a subsequent read function, scanf ("%s", str) is read from the 11th character.
There is a problem with actually using the scanf () function, which is illustrated in the following example:
When you use multiple scanf () functions to enter multiple character variables consecutively, for example:
Main ()
{
Char C1, C2;
scanf ("%c", &AMP;C1);
scanf ("%c", &AMP;C2);
printf ("C1 is%c, C2 is%c", c2\1, C2);
}
Run the program, enter a character a after carriage return (must enter to complete input), when executing scanf ("%c", &AMP;C1), assign "A" to the variable c1, but the carriage return is still in the buffer, executing the input statement scanf ("%c", &AMP;C2), Variable c2 output is a blank line, if the input ab back to return, then the output will be: C1 is A, C2 is B.
To resolve the above problem, you can add the Purge function fflush () before the input function (the use of this function is described at the end of this section). Modify the above program to become:
#include <stdio.h>
Main ()
{
Char C1, C2;
scanf ("%c", &AMP;C1);
Fflush (stdin);
scanf ("%c", &AMP;C2);
printf ("C1 is%c, C2 is%c", C1, C2);
}
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.