You get a bonus - 1 coin for daily activity. Now you have 1 coin

8.4. Formatted I / O.

Lecture



I / O tasks in the C language are solved by the printf () and scanf () functions.
Output operators are used to display the values ​​of variables and text.
Texts in C are written in quotes. There is no special type for working with strings in the C language. Instead, strings are represented as an array of type char . The elements of a string, stored in an array, are written to adjacent memory cells. At the end of the line put a mark \ 0 . The presence of this mark indicates that the line must be one size larger than the number of characters. A string is declared by specifying the char type and writing the size to [] .
Example:
char name [40];
To work with strings, you must connect the string.h file to the program.
Each of the I / O functions uses a control string and a list of arguments. The control string is written first and is enclosed in quotes. Control strings and arguments are not shuffled. The control lines first of all contain an indication of the output of a value of one or another type. They are called conversion specifiers .

Qualifiers Actions
% a,% A returns a floating point number and 16th digits
% c returns a single character
% d,% i return an integer in decimal
% e% e return a floating point number in exponential form
% f returns a float in decimal
% g uses the% f or% e specifier depending on the value (automatically selected)
% o returns an octal unsigned integer
% p pointer output in hexadecimal
% s returns character string
% u returns unsigned decimal integer
% x% x return unsigned hexadecimal integer
%% print the character%


The main specification can be modified by inserting modifiers between the% and the conversion character.

Modifiers Actions
number / digits minimum width of the output field (the number of screen characters)
.numeral accuracy
h used in integer conversions to encode short int and unsigned short int values
hh used in integer conversions to encode signed char and unsigned char values
j used in integer transformations to obtain the types int_max_t and uint_max_t
l used in integer transformations to get the type long int and unsigned long int
ll used in integer transformations to get the type long long int and unsigned long long int
t used to encode ptroliff_t values, which is the difference of two pointers
z used to encode size_t values
L used when converting floating point numbers to long double
flag + Values ​​with a sign are printed with a "+" or "-"
flag - left-aligned item
flag space the value with the "+" sign is printed with a space, but without a sign
flag # for the% o,% x,% X specifiers, non-significant zeros are output. For floating point forms, a dot is guaranteed to be printed, even if there are no decimal places.
flag 0 for numeric forms, instead of spaces, zeros are displayed on free screen spaces.


Data entry is performed using the scanf () function. There are several input functions in the C language. scanf () is used more often than others because It has a different input format. Like the printf () function, input uses a control string followed by a list of arguments. The control string indicates which formats the input text should be converted to. As parameters, the scanf () function uses not the variables themselves, but pointers to them. To use scanf () you need to follow two rules:
1) if the function is used to read values ​​into a variable of the main type, then the name of the variable is preceded by a & .
2) if the value is read into a character array, then the & character is not put.
Conversion of a single input data is performed by specifying a conversion specifier . If more than one data is entered, they are separated by a space character.

Qualifiers Actions
% c interprets the input as a character
% d interprets the input as a decimal signed integer
% e,% f,% g,% a interprets the result of the input as a floating point number
% E,% F,% G,% A interprets the result of the input as a floating point number. All entered characters are converted to uppercase.
% o interprets the input as an octal signed integer
% s interprets the input as a pointer
% s interprets the input as a string. Input starts with the first non-service character and includes all characters until the next service character.
% u interprets the input as an unsigned integer
% x% x interprets the input as a signed hexadecimal integer


Modifiers can be used between the% sign and the transformation symbol. If there are more than one, they should be recorded in the order indicated in the table below:

Modifiers Actions
* suppress assignment
number / digits maximum width of the input field. The input stops when the specified width is reached or upon detection of a service character.
hh specifies the reading of an integer as signed char or unsigned char
ll defines the reading of an integer as long long or unsigned long long
L, l, h defines the reading of a number as short int, unsigned short int, unsigned long, long, double or long double



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithmization and programming. Structural programming. C language

Terms: Algorithmization and programming. Structural programming. C language