C Language Learning Series: Input & Output

Input namely padding data program.

Output that is on the screen, the display data file or the printer.

Standard file

C language to all devices as files, and this is similar to Linux.

The following three files will automatically open when the program is executed, in order to access the keyboard and screen (file pointer is a way to access the file).

Standard file File pointer device
Standard Input stdin keyboard
Standard output stdout screen
Standard error stderr Your screen

scanf () & printf () function

scanf()

int scanf(const char * restrict format,...);

Function scanf () read the content, format description read a plurality of characters, stored in the variable and the corresponding address from the standard input stream stdin. 

The first parameter is a function of the format string that specifies the format of the input, and the format specifiers parse input and stored in the position corresponding to the variable parameter list (all the parameters except the first parameter) in the corresponding pointer position. Each non-null pointer claims, and sequentially correspond to eleven string formatter.

scanf function returns the number of items successfully read ( but the asterisk modifier and read into the unassigned domains are not counted ) encountered when data is read "End of File" or an error is encountered EOF (EOF is returned value is -1, using bitwise ~ (-1 complement is a per 1) is 0).

supplement:

Original code is the highest bit is the sign bit, the bit number n is 0, the bit is a negative number, the remaining bits of bit values, (0 has two representations: +0 and -0), the size of the remaining bits representing a number

The complement is a positive integer whose binary representation, the same as the original code. Complement negative integer, which is the original code to all bits except the sign bit inverted plus 1. 0 Number complement representation is unique , and each is 0.

Format Explanations:

1. *

Represents read data will be discarded. * Format command does not correspond with any data variable in the parameter list.

2. field width

It appears in the form of a non-zero decimal integer. It represents the maximum number of characters of the read instruction format.

3. The format specifier

C character sequence reads wide-field specified number of characters (hereinafter not add null bytes), if it is omitted, the width of a single character is read. The% c% 1c or read a single character,% 2c reads two characters (not add null bytes later), and so on.

s read a sequence of characters, the null bytes will be added later, encounters a blank character (\ t \ r \ n space, etc.) to complete the read.

d reads optionally signed (optionally signed with the symbol represents the input may be unsigned, without sign is considered non-negative) decimal integer.

u read symbol unsigned decimal integer.

i reads optionally signed integer.

a, e, f, g, A, E, F, G reads optionally signed floating point number.

o Optional read a signed octal integer.

x, X reads optionally signed hexadecimal integer.

a read pointer value p.

n does not read any character, but to the position of the number of characters read is stored in the location pointed int * corresponding thereto.

%% Symbol is read.

[] Matches the character set read scan into an array of characters. Scanning a set of defined set of characters, may scanf () which allow the read characters and assigns a corresponding array of characters. Scan collection is defined by a string of characters in square brackets, front left bracket must be decorated with a percent sign. For example% [ABC],% [AZ], can be described by the character ^ complement. When scanning set, Scanf () continuously eat into set of characters corresponding to the character array and placed, until it finds a character not in the set (i.e., current scan matches the character read only). Return, to the end of the array disposed null, the string of characters read.

4. The length modifier

hh and d, i, o, u, x, X, or n with the use of a signed char indicate corresponding or unsigned char data.

h and d, i, o, u, x, X, or n with the use of a short int indicate corresponding or unsigned short int data.

l and d, i, o, u, x, X, or n with the use of a long int indicate corresponding or unsigned long int data; and a, A, e, E, f, F, g, or G with a double data representing correspondence; and c, s, [with the use of data representing the corresponding wchar_t.

ll and d, i, o, u, x, X, or n with the use of a long long int indicate corresponding or unsigned long long int data.

j and d, i, o, u, x, X, or n with the use of a intmax_t indicate corresponding or uintmax_t data.

z and d, i, o, u, x, X, or n with the use of a size_t designate corresponding data (or corresponding size_t signed integer data).

and t d, i, o, u, x, X, or n with the use of a ptrdiff_t designate corresponding data (or corresponding ptrdiff_t unsigned integer).

And L a, A, e, E, f, F, g, or G with the use of a long double designate corresponding data.

printf()

int printf(const char *format, ...);

The function returns an integer value. If successful, it returns the number of characters output, the output error is a negative return.

Call form: printf ( "<format string>", <argument list>);

Wherein the format string includes two parts: part of normal characters, which will be output as; the other is a predetermined character format, with "%" and followed by one or more predetermined characters, used to determine the output format of the content . Parameter table is a series of parameters required to be output, as many as the number of output parameters that the number must be formatted string described, with each parameter between "," separated, and the order of one to one.

Specifier

% D decimal signed integer

Unsigned decimal integer% u

% F float

% S string

% C single character

% P value of a pointer (address)

Floating-point exponential% e

% X,% X unsigned integer expressed in hexadecimal

% O unsigned integer in octal representation

% G output from the output values ​​of the small length in accordance with the% f% e, or output mode type

% P output address character

% Lu 32-bit unsigned integer

% Llu 64-bit unsigned integer

1) the maximum digital representation may be inserted between a wide field of "%" and the letter. If the width of the length field, bits integer or string exceeds described, the output of its actual length . Floating-point, integer part if the bit width of an integer number of bits than described, will be the actual output bit integer; if the fractional part of decimal digits than the bit width of illustration , the width of the press illustrated in rounded output. If a floating point or integer quantity representing the character output format, the number after the decimal point represents the maximum width, in front of the decimal numbers represent the minimum width.

2) n newline \ f clear the screen and page \ r carriage return \ t Tab character

3) may control the output of the left or right aligned, i.e., added between a "%" and the letter "-" sign may indicate output left justified, or right justified.

4) can be added between the lowercase letter l "%" and the letter indicates the number of the output is long.

getchar () & putchar () function

getchar()

int getchar(void);

getchar achieved by the macro: #define getchar () getc (stdin) , the function is declared in  stdio.h  when the header file, used to include stdio.h header file. .

getchar has a return value of type int. getchar function returns the ASCII value of the character input by the user, if the end of file is returned -1 (EOF), and the character input by the user back to the screen.

When the program calls getchar, program waiting for user keys. Characters entered by the user is stored in the keyboard buffer. Up until the user press Enter

When you press Enter, getchar began to read a character from stdin stream each time. If the user entered before pressing Enter to more than one character, other characters remain in the keyboard buffer area, waiting for the follow-up getchar calls read. In other words, the subsequent call to getchar will not wait for a key, and direct buffer character read, after reading until the buffer characters, only wait for a key.

putchar()

int putchar(int char);

(1) When the char (the English state) of a single quotes to cause a character, outputting the character (Note: the character can also be an escape character);

(2) when the char is a decimal integer between 0 to 127 (including 0 and 127) interposed, it will be regarded as corresponding to the ASCII code for the character, outputting the character corresponding to the ASCII code;

(3) When the char during char with a previously defined string variables, the output character variable points.

gets () & puts () function

gets()

gets () function to read a string until the line feed (from the standard input device (keyboard) \ n-(we enter a carriage return) ) ends, the newline character is discarded, and then add '\ 0' character at the end.

char *gets(char *str);

Wherein str a string variable (or string array name string pointer)

return value:

Successfully read, returns the same parameter buffer pointer;

Read encountered EOF or an error occurs, returns a NULL pointer. So in the case met the return value is NULL, use ferror or feof function checks the error occurred or encountered EOF.

gets (s) and function scanf ( "% s", s ) similar to, but not identical, to use scanf ( "% s", s ) there is a problem when an input string functions, that is, if the input string that will be spaces end, a space character after the processing as the next entry, but gets () function receives the entire input string until it encounters the stop behavior change.

puts()

the puts () function is used to the standard output device (screen) and line output string, in particular: the output string to the standard output device, '\ 0' to a carriage return.

int puts(const char *str);

Wherein str a string variable (or name string pointer string array);

The output string to the terminal, the puts function can only output a string of characters may include a transfer.

action puts (s) and the effect of the statement printf ( "% s", s ) substantially the same, the puts () function can only output string, output values or not format conversion. the puts () function will automatically output a carriage return to the output string .

Published 161 original articles · won praise 90 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104027452