Chapter IV keyboard input and screen output
Escape character
\n | Wrap, move the cursor to the starting position of the next line |
\r | Carriage return (not wrapped), move the cursor to the beginning of the current row |
\0 | Null character |
\t | Horizontal tab |
\ v | Vertical tab |
\b | backspace |
\f | The paper feed |
\a | Ringing alarm tone |
\" | A double quotation mark |
\' | A single quotation mark |
\\ | A backslash |
\? | question mark |
\ddd | Characters 1-3 octal ASCII value represented by |
\ socialization | 1-2 character hexadecimal ASCII value represented by |
Each character has a value equivalent corresponding thereto integer, the integer value is the ASCII code of the character
From this sense, char type can be viewed as a special type of int
Char and int data type to the data type conversion does not return information is lost
Unicode-- International Organization for Standardization to develop a more powerful coding
// L4-1
#include <stdio.h>
main ()
{
char CH;
the printf ( "Press A and the then Press the Enter Key:" );
CH = getchar (); / * a character input from the keyboard, press the Enter key input end, the * character is stored in the variable ch / ch ch + = 32; / * uppercase letters lowercase letters * / the putchar (ch); / * display variable ch characters * / on the screen putchar ( '\ n '); / * output a carriage return linefeed control characters * / }
// operating results
Press A and the then Press the Enter Key: B
b
Function getchar () and the putchar () is a function of the C standard library dedicated character input / output
Function getchar () no arguments
printf (format control string, the output value of the parameter table)
Format control string comprising: a format conversion specification, ordinary character is output as required
// L4-2
#include <stdio.h>
main ()
{
char CH;
the printf ( "Press A and the then Press the Enter Key:" );
CH = getchar (); CH = CH + 32 ; the printf ( "% C,% D \ n- ", ch, ch); / * output the characters and the ASCII value of the variable ch * / }
// Run Results
Press and the then Press the Enter Key A: B
B, 98
The printf () format modifier
- l:long
- L:long double
- h:short
- m (m is an integer): Specifies the field width (number of columns occupied by the output items output)
- .n (n is a nonnegative integer): Displays precision
// L4-3
#include <stdio.h>
main ()
{
const Double PI = 3.14159 ;
Double R & lt, circum, Area;
the printf ( "R & lt the Input:" ); Scanf ( "% LF", & R & lt); circum = 2 * PI * R & lt; Area = PI * R & lt * R & lt; the printf ( "the printf the WITHOUT width or Precision Specifications: \ n-" ); the printf ( "Circumference =% F, Area =% F \ n-" , circum, Area); the printf ( "the printf precision Specifications width and the WITH: \ n-" ); the printf ("% 7.2f = Circumference, Area =% 7.2f \ n-", circum, Area); / * use and precision specifier field width, field width is occupied by data 7, display accuracy of 2 * / }
// operating results
Intput r: 6
printf width or the WITHOUT Precision Specification:
Circumference = 37.699111, 113.097334 Area =
printf width or the WITH Precision Specification:
Circumference = 37.70, Area = 113.10 // Note, also accounted for a decimal point character position
scanf (control string format, the parameter address table)
When using scanf () input numeric data, you encounter the following situations will agree to end data entry
- In case of spaces, carriage returns, tabs
- Reaches the input field width
- In case of illegal character input
If the function scanf () format control characters other than the format specifier present in the string, the characters must be input by the user from the keyboard when the input data
// L4-4
#include <stdio.h>
main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("a = %d, b = %d\n", a, b);
}
Results // Run
12 is 34 is
A = 12 is, B = 34 is
scanf ( "% d% * c% d", & a, & b); allows the user to input data as a delimiter character arbitrary
When scanf () return the specified number of data items representing the function is called success
When scanf () Returns the value of EOF indicates failure function is called
// L4-5
#include <stdio.h>
main()
{
int data1, data2;
char op;
printf("Please enter the expression data1 + data2\n");
scanf("%d%c%d",&data1, &op, &data2);
printf("%d%c%d = %d\n", data1, op, data2, data1+data2); }
// Run Results
Please Enter The expression The DATAl + DATA2
12 is. 3 +
12 is. 3 + 15 =
// L4-6
#include <stdio.h>
main ()
{
int A;
char B;
a float C; the printf ( "Please Integer AN INPUT:" ); Scanf ( "% D", & A ); the printf ( "Integer:% D \ n- " , a); the printf (" Please INPUT a character: " ); Scanf ("% c ", & B); / *% in front of a space c, stored in the buffer carriage read * / the printf ( "Character:% C \ n-" , B); the printf ( "Please INPUT A a float Number:" ); Scanf ( "% F", & C); the printf ( "a float:% F \ n-" , C) ;}
//运行结果
Please input an integer:12
integer: 12
Please input a character:a
character: a
Please input a float number:3.5
float: 3.500000