Keyboard input and screen output C language

Keyboard input and screen output is most commonly used when writing simple sequential structure of the program operation. Keyboard input and screen output is the C program by calling the input / output functions implemented. This chapter describes the common input / output functions.
A, single character input / output
1. The character constant
a character character constant in the C language is enclosed in single quotes. For example, 'a' is a character constant, and a is an identifier. Again, '3' represents a character constant, while 3 indicates an integer.
In practice the character of a pair of single quotes, for most printable characters, but not for certain control characters (such as carriage return, line feed, etc.). Thus, C language also introduces another special form of a character constant escape character (Escape Character), which is a backslash (\) character sequence at the beginning, which has a specific meaning, for the purpose of describing particular control characters, and use the same to put inside single quotes. As used escape character '\ n', '\ t '.
Escape character table in the following table.

Character Meaning Character Meaning
'\ n' new line '\ A' alarm bell sound
'\ r' carriage (not wrapped) '\ "' a double quote
'\ 0' null character, commonly used as end of string flag ' \ '' single quote
'\ t' horizontal tab '\' a backslash
'\ V' vertical tab '?' question mark
'\ B' backspace '\ ddd' 1 to. 3 digit octal ASCII value represents character
character '\ f' the paper feed '\ xhh' 1 to 2-digit hexadecimal ASCII value represented by

For example, the character program example earlier chapters involved '\ n', is a kind of escape character, which is used when a line feed process control output, i.e. to the starting position of the broadcast of the next line. And '\ n' is different from the, '\ r', said carriage return, line feed, but not about to move to the broadcasting start position of the current line. And '\ t' horizontal tabs, press the TAB key equivalent.
Also worth noting is that: when the escape sequence in the string, is based on a single character count. For example, the length of the string "abc \ n" is 4, not 5 because the character '' \ n 'represents 1 characters.
Since the character variables accounted for only one-byte memory space, so it can only store one character. Character variable range depends on the character set used by the computer system. Currently characters on a computer that is widely used in ASCII (American Standard Code for Information Interchange) character set (the character table may reference software). It specifies the character set corresponding to each character code, i.e. the character sequence "serial number." That is, each character has a value equivalent to its corresponding integer, this integer value is the ASCII code of the character. From this sense, char type can be viewed as a special type int.
An integer number is stored in binary form in memory, and memory is also a character in its binary form of ASCII code corresponding to the store. For example, the character 'A' is stored in memory whose ASCII code binary value of 65, the integer storage format similar to 65, but a different number of bytes in the memory occupied by it. char data type is 1 byte.
In the ASCII code range, the data type of char and an int type data conversion information is not lost, the two may be mixed operation. At the same time, both a char data type character to the output format, can be output in integer format, the output format is directly output to an integer value whose decimal ASCII code.
2. Character input / output
getchar () putchar () is a function in the C standard library dedicated to the character input / output. The putchar action function () is output to a character position of the current broadcasting screen. The action function getchar () is implicit from the system specified input device (keyboard about to end) to enter a character, press the Enter key to indicate the end of input, read character will automatically be echoed to the screen.
[11.1] cases from a keyboard input capital letters, it is converted to lowercase, and then reality to the screen.
[Problem Solving Analysis To observe the Appendix D of the common ASCII character set, can be found in such a law, i.e. ASCII value lowercase letters ASCII value than the corresponding capital letters large 32, i.e., 'a' and 'A' , 'b' and 'B', 'c' and 'C' ...... the difference values are 32 ASCII code. According to this law, it can easily convert between case letters. Procedure is as follows:
#include "stdio.h"
void main ()
{
char CH;
the printf ( "Press A and the then Press Enter Key:");
CH = getchar (); / keyboard input from a character, press Enter to complete , the character into the variable ch /
ch = ch + 32; / uppercase to lowercase converted /
the putchar (ch); / display variable ch characters on a large screen /
the putchar ( '\ n-'); / output a carriage return linefeed control characters /
}
run results of the program are as follows:
A Key and the then Press Press Enter: B
B
program statement first Line 6 calls the function getchar () inputs a character from the keyboard, then the character is read function getchar () returns a character value is assigned to the variable ch. Note that the function getchar () has no parameters, return value of the function is read from the terminal keyboard characters. Thus, the statement is not to be written as:
getchar (ch); / wrong use /
Line 7 statement ch ASCII value of the variable capital letters plus 32, to give the corresponding lowercase letters ASCII value, in order to achieve capital letters to lowercase letters conversion. Since the character 'a' character 'A' subtraction, corresponding to the character 'a' character 'A' is subtracted ASCII code, also corresponding to the character 'b' and the character 'B' is subtracted ASCII code ... ... and subtract the difference between the two is 32, so line 7 and the statements following statements are equivalent;
CH = CH + ( 'a' - 'a');
line 8 statement invokes the putchar function () to the terminal ch output current cursor position in the screen of the display character, the putchar function () is a parameter to be outputted from the character, the character is a printable character either, may also be an escape character.
Second, screen output data formatting
function printf () format is generally
printf (format control string);
printf (format control string, the output value is a parameter table);
Wherein the control string format (format string) is a character string enclosed in double quotes, also known as converter control character string, the output value of the parameter table may have a plurality of output values may not (output only when a string). In general, the format control character string consists of two parts: the format conversion specification (Format Specifier) and ordinary characters as required output. As shown in Table 4-2, illustrate the format conversion% begins and ends with a character conversion (Conversion Character), each output value of the parameter is used to specify an output format.
Common function printf () format conversion described
format conversion demonstrates the use of
a decimal integer with a sign of the output of% d, the positive sign is omitted
% c output a character
% s output string
% f outputs a real number in decimal form decimal
%% output percentage number%%

List of data items required output value is output from the parameter table, the variable output data item may or expression, separated by commas between the output values of the parameters, the type and format conversion specifier should match. Each output format conversion specification parameter values and the input symbol values in the parameter table one correspondence, there is no output when the value of the parameter, string format control specifier format conversion is no longer required.
[Example 4.2] input from the keyboard a capital letter, convert it to the lowercase letters, the lowercase letters converted decimal value of the ASCII code and displayed on the screen.
#include "stdio.h"
void main ()
{
char CH;
the printf ( "Press A and the then Press Enter Key:");
CH = getchar (); / keyboard input from a character, press Enter to complete the character into the variable ch /
ch = ch + 32; / uppercase to lowercase converted /
the putchar (ch); / display variable ch characters on a large screen /
the printf ( "% C,% D \ n-", ch, CH);
}
run results of the program are as follows:
Press the then a and Press Enter Key: B
B, 98
runs as follows:
Compared with the procedures in Example 11.1, as compared to the program use the formatted output function printf () function to output putchar character, and the character output () function printf () are the advantages of both character format (% c) also It can be an integer in decimal format (% d) value (e.g., the program shown in row 8) output char type variable. Integer in decimal format (% d) is output when the output value of the ASCII code char type variable. Here, the effect of the following two statements are equivalent.
the printf ( "% C", CH);
the putchar (CH);
and the role of the following two statements are equivalent.
; ( "\ n-") the printf
the putchar ( '\ n-');
three, keyboard input data format
general format 1. function scanf () of
scanf (control string format, the parameter address table);
wherein the format control characters string is enclosed in double quotes string, comprising format conversion instructions, and delimiters two portions. Function scanf () format conversion specifier (Table 4-4)% usually begins and ends with a character format for specifying each parameter input format.
Illustrate the use of format conversion
% d input decimal integer
% c input a character, blank characters (including spaces, carriage returns, tabs) are valid characters
% s input string encountered blank characters (including spaces, carriage returns, tabs when the operator), the system reads think the end.
% f input real numbers, or a decimal exponent inputs can
%% %% Percent output

parameter address list is a list of addresses consisting of several variables, these parameters are separated by commas. Function scanf () must specify the required address of the variable to receive data, the data can not be read or the memory cell specified correctly.
2. Format modifier function scanf () in
the printf () Similarly, in%, and intermediate formatter function scanf () format modifier may also be inserted.
When () function with numeric data input scanf, we encountered the following situations are considered to end data entry:
l In case of spaces, carriage returns, tabs (the TAB)
l reaches the input field width;
l case of illegal character input .
Note that, if the function scanf () format control string format described inter presence characters other than character, then the character must be input from the keyboard by the user when inputting data.
[Example 4.4] following the procedure used to demonstrate function scanf () format requirements of the input data.
#include "stdio.h"
void main ()
{
int A, B;
Scanf ( "% D% D", A &, & B);
the printf ( "% A = D, B = D% \ n-", A, B );
}
when the required program output is a = 12, when 34 b =, how user input data?
A: Because two format conversion program statement in line 5 is the spaces between the character described, as an ordinary character, it must be input when the user input, i.e., a space should be input as the separator between data. So in this case the input data according to the following format:
12 34
when defining user input data to Comma Separated, i.e., when the input data is defined in the following format, which shall be changed in the program statement? How do you change?
12,34
A: If required between input data separated by a comma, must be separated by commas, i.e. should the program statement line 5 between the two modified format conversion specifier
scanf ( "% d,% d ", & a, & b);
when the program statement line 5 sentence modified as follows, how the user input data?
Forget to take when using the foregoing variables plus function scanf () address operator to specify an address to receive data variable, which is a common mistake for beginners.
Common examples of common errors Error Description Error Type
printf ( "the Input A:");
Printf ( "the Input A:");. The printf () mistakenly written as print () or Printf () link error
printf ( "Input a :) ;
Scanf (% D ", & a); forgot to a printf () or scanf () in the format control character string with quotation marks compile error
Scanf ("% D, "& a);
the printf (" a =% D, " a); a comma-separated format control character string expression and format control character string written within a compile error
Scanf ( "% d", a ); forget to scanf () variable in the prompt for adding address-warning
the printf ( "a =% d \ n" ); forgot to write error output variable parameters runtime
scanf ( ".% 2f", & c); with scanf () when entering real data accuracy runtime error format control string specified

Guess you like

Origin blog.csdn.net/SqrsCbrOnly1/article/details/91361750