From

xiaoxiao2021-03-06  158

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

Data input statement

The C language data input is also done by the function statement. This section describes the function scanf and getchar input data from the standard input device-keyboard. The scanf function scanf function is called a format input function, which is in the format specified by the user from the keyboard to the specified variable.

First, the general form of the scanf function

The Scanf function is a standard library function, which is the same in the header file "stdio.h", which is the same as the Printf function, and the C language also allows you to include the stdio.h file before using the Scanf function. The general form of the ScanF function is: Scanf ("Format Control String", address list column); where the format control string is the same as the Printf function, but the non-format string cannot be displayed, that is, the prompt string cannot be displayed. The address of each variable is given in the address table column. The address is composed of the address operator "&" followed by the variable name. For example, & A, & B represents the address of the variables A and the variable B, respectively. This address is the address that the compilation system is assigned to the A and B variables in memory. In the C language, the concept of address is used, which is different from other languages. The value of the value of the variable and the address of the variable should be distinguished from different concepts. The address of the variable is the Association system allocation, and the user does not have to care about the specific address. The relationship between the address and variable values ​​of the variable is as follows: & a ---> A567 A is the variable name, 567 is the value of the variable, & a is the address of the variable A. Assign the variable to the variable in the assignment expression, such as: a = 567 is the variable name on the left side of the assignment number, cannot write the address, and the scanf function is in nature to assign a value to the variable, but requires the address of the write variable, such as & a. Both are different in form. & Is an address operator, & A is an expression, its function is the address of the variable. Void main () {Int A, B, C; Printf ("INPUT A, B, C / N"); Scanf ("% D% D% D", & A, & B, & C); Printf ("A =% D, B =% D, C =% D ", A, B, C);} Note & Usage! In this example, since the SCANF function itself cannot display a prompt string, first use the printf statement on the screen on the screen. Please enter the value of A, B, and C. Exit the Scanf statement, exit the TC screen to enter the user screen waiting for the user to enter. When the user enters 7, 8, 9, press the Enter key, at which time, the system will return to the TC screen. In the format string of the scanf statement, since there is no non-format character, the interval is input between "% D% D" D% D, so it is necessary to use more than one space or the Enter key when entering. Between the two inputs. Interval. Such as: 7 8 9 or 789

Format string

General forms of format strings are:% [*] [Enter Data Width] [Length] Type where square bracket [] is optional. The significance is as follows: 1. Type indicates the type of input data, its formatting and meaning shown below. Format Character Sign D Enter Ten Import Integer O Input Octa Integer X Enter Hexadecimal Entario 10 No Symbol Decimal Integer F or E Enter Truth (Forms) C Enter a single character S Enter string 2. " * "The value is used to indicate that the input item does not give the corresponding variable after reading, that is, skip the input value. Such as Scanf ("% D% * D% D", & A, & B); when the input is: 1 2 3, 1 gives 1 to a, 2 is skipped, 3 give B. 3. The width uses the decimal integer specifies the width of the input (ie, the number of characters). For example: SCANF ("% 5D", & A); input: 12345678 Give only 12345 to the variable A, the remainder is cut off. Another example: scanf ("% 4D% 4D", & A, & B); input: 12345678 will give 1234 to A, and give 5 b. 4. The length of the length format is L and H, and L represents the input length integer data (such as the% LD) and the double precision floating point number (as% LF). h represents the short integer data. You must also pay attention to the following: a. There is no precision control in the scanf function, such as Scanf ("% 5.2f", & a); is illegal. You cannot try to use this statement to enter a decimal number of 2 digits. b. SCANF is required to give a variable address, if the variable name will be wrong. Such as scanf ("% d", a); is illegal, it should be changed to SCNAF ("% D", & a); it is legal. c. When entering multiple numerical data, if there is no non-format character in the format control string, there is a space, TAB, or a carriage return. C compiles the space, Tab, carriage return, or illegal data (if "12A" input "12A" is input to "% D", that is, it is considered to end. d. When entering the character data, if there is no non-format character in the format control string, all the characters that all entered are valid characters. For example: SCANF ("% C% C% C", & A, & B, & C); input to: D e f, gives 'D' to A, 'F' Give B, 'E' Give C. On only when the input is: DEF, the 'D' is assigned to a, 'E' gives C. If you add spaces in the format control as spacing, such as Scanf ("% C% C% C", & A, & B, & C); then add space between each data when input. Void main () {Char A, B; Printf ("Input Character A, B / N"); Scanf ("% C% C", & A, & B); Printf ("% C / N", A, b); Mn, the result is only M.

When the input is changed to Mn, the MN can be output, see the following input operation: Input Character A, BMnmnvoid main () {Char A, B; Printf ("Input Character A, B / N"); scanf (" % C% C ", & A, & B); Printf (" / n% C / N ", A, B);} Scanf ("% C% C ", & A, & B); this example represents SCANF format control When there is a space between the string "% C% C", the input data can be spaced apart. e. If there is a non-format character in the format control string, you should also enter the non-format character. For example: SCANF ("% D,% D,% D", & A, & B, & C); in which the non-format "," is separated, the input should be: 5, 6, 7, such as: scanf ( "A =% D, B =% D, C =% D", & A, & B, & C); then the input should be A = 5, b = 6, c = 7g. If the input data is inconsistent with the output type Although the compilation can pass, the result will be incorrect. Void main () {int A; Printf ("Input A Number / N"); Scanf ("% D", & A); Printf ("% ld", a);} Due to the input data type is integer, output The format string of the statement is explained as a long integer, so the output result and the input data do not match. If the change program is as follows: void main () {long a; printf ("Input A long integer / n"); scanf ("% ld", & a); printf ("% ld", a);} Run results: Input a long integer12345678901234567890 When the input data is changed to a long integer, the input and output data is equal. The keyboard input function getChar function getchar function is entered from the keyboard. Its general form is: getChar (); usually gives an input character to a character variable, constitutes an assignment statement, such as: CHAR C; c = getchar (); # include void main () {char C; printf ("Input a character / N"); c = getchar (); putchar (c);} You should also pay attention to several issues using the getchar function: 1.GetChar function can only accept a single character, and enter the number and press the character. When you enter more than one character, only the first character is received. 2. You must include file "stdio.h" before using this function. 3. When running under the TC screen, you will exit the TC screen into the user screen waiting for the user to enter. The input is completed and then returned 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 their 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));} Outputs the byte length of various data types.

转载请注明原文地址:https://www.9cbs.com/read-128453.html

New Post(0)