format string
The general form of the format string is:%[*][input data width] [length] type with parentheses [] as an option. The meaning of the items is as follows:
1. Type
Represents the type of input data, with its format character and meaning as shown in the following table.
Format character meaning
D Enter a decimal integer
o input octal integer
x Enter a hexadecimal integer
U Enter unsigned decimal integer
F or e input real number (in decimal form or exponential form)
C Enter a single character
s input string
2. "*" character
To indicate that the input item does not give the corresponding variable when it is read, that is, skip the input value. such as scanf ("%d%*d%d", &a,&b) when entered as: 1 2 3 o'clock, give 1 to a,2 is skipped, 3 gives B.
3. Width
Specifies the width of the input (that is, the number of characters) with a decimal integer. For example: scanf ("%5d", &a);
Input:
12345678
Assign only 12345 to variable a, and the remainder is truncated. Also such as: scanf ("%4d%4d", &a,&b);
Input:
12345678 will assign 1234 to a and 5678 to B.
4. Length
A length format character of L and H,l represents the input of long integer data (such as%LD) and a double-precision floating-point number (such as%LF). h indicates the input of short integer data.
The following points must also be noted with the scanf function:
A. There is no precision control in the scanf function, such as: scanf ("%5.2f", &a); is illegal. You cannot attempt to enter a decimal number of 2 digits with this statement.
B. scanf requires that the variable address be given, such as an error if the variable name is given. If scanf ("%d", a); is illegal, should be changed to Scnaf ("%d", &a);
C. When entering multiple numeric data, if there is no unformatted character in the format control string for the interval between the input data, you can use a space, a tab, or a carriage return interval. C compilation is considered to end when it encounters a space, TAB, carriage return, or illegal data (such as an illegal data when "%d" is entered "12A").
D. When entering character data, if the format control string is nothing more than a format character, it is assumed that all characters entered are valid characters. For example:
scanf ("%c%c%c", &a,&b,&c);
Enter as:
D E F
Give a, ' F ' to give B, ' E ' to C. Only when input is:
Def
, you can assign ' d ' to A, ' E ' to give B, ' F ' to C. If you add a space as an interval in format control, such as scanf ("%c%c", &a,&b,&c), you can add spaces between the data when you enter.
void Main () {
Char a,b;
printf ("Input character a,b\n");
scanf ("%c%c", &a,&b);
printf ("%c%c\n", a,b);
}
scanf ("' C14f14%c%c", &a,&b);
printf ("%c%c\n", a,b); Because there is no space in the scanf function "%c%c", input m N, the result output is only M.
When the input is changed to MN, you can output MN two characters, see the following input operating conditions: input character a,b
MN
MN
void Main () {
Char a,b;
printf ("Input character a,b\n");
scanf ("%c%c", &a,&b);
printf ("\n%c%c\n", a,b);
}
scanf ("%c%c", &a,&b); This example indicates that when there is a space between the scanf format control string "%c%c", there can be a space interval between the data entered. E. If you have unformatted characters in the format control string, enter the formatting characters as well.
For example:
scanf ("%d,%d,%d", &a,&b,&c); It uses the non-format character "," as the spacer, so the input should be: 5,6,7
Also such as: scanf ("a=%d,b=%d,c=%d", &a,&b,&c);
The input should be
a=5,b=6,c=7g. If the data entered is inconsistent with the type of output, the compilation can pass, but the result will not be correct.
void Main () {
int A;
printf ("Input a number\n");
scanf ("%d", &a);
printf ("%ld", a);
}
Because the input data type is an integral type, and the output statement's format string is described as a long integer, the output is inconsistent with the input data. If the change procedure is as follows:
void Main () {
Long A;
printf ("Input a Long integer\n");
scanf ("%ld", &a);
printf ("%ld", a);
}
The results of the operation are:
Input a long integer
1234567890
1234567890 when the input data is changed to a long integer, the input and output data are equal.
Keyboard Input function
The function of the GetChar function GetChar function is to enter a character from the keyboard. Its general form is: GetChar (); A character variable is usually assigned to an input character, which forms an assignment statement, such as:
char c;
C=getchar ();
#include <stdio.h>
void Main () {
char c;
printf ("Input a character\n");
C=getchar ();
Putchar (c);
}
There are several issues to be aware of using the GetChar function:
The 1.getchar function accepts only a single character, and the input number is processed by character. When you enter more than one character, only the first character is received.
2. You must include the file "Stdio.h" before using this function.
3. When running the program with this function under the TC screen, the TC screen will exit to the user screen and wait for user input. Enter the finish and return to the TC screen.
void Main () {
Char a,b,c;
printf ("Input character a,b,c\n");
scanf ("%c%c,%c", &a,&b,&c);
printf ("%d,%d,%d\n%c,%c,%c\n", a,b,c,a-32,b-32,c-32);
}
Enter three lowercase letters
Output its ASCII code and corresponding uppercase letters.
void Main () {
int A;
Long B;
float F;
Double D;
char c;
printf ("%d,%d,%d,%d,%d", sizeof (a), sizeof (b), sizeof (f)
, sizeof (d), sizeof (c));
}
Output byte lengths for various data types.