C language basics - input and output

Table of contents

Standard format input and output

enter:

Format control string:

Address table entry:

Differences in format strings (scanf and printf):

*Modifier:

The return value of scanf():

Output:

Format control string:

Output table items:

Commonly used output formats and meanings:

Regarding the return value of printf():

Character input and output

Input function:

Output function:

String input and output

enter:

working process:

Notice:

Output:


Standard format input and output

Input :

        scanf(), according to the format of the format string, input data from the keyboard into the specified variable

Format control string:

        Cannot display prompt string

Address table entry:

        The address in the address table entry gives the address of each variable. The address is composed of the address operator "&" followed by the variable name.

Differences in format strings (scanf and printf):

        (1) In the format specifier, the width of the data can be specified, but the precision of the data cannot be specified.               

        (2) %ld must be used when inputting long type data, and %lf or %le must be used when inputting double data.

        (3) Append the format specifier "*" so that the corresponding input data is not assigned to the corresponding variable

        The conversion specifiers used by the scanf() function are almost identical to those used by printf(). The main difference is that printf() uses %f, %e, %E, %g, and %G for both float and double types, while scanf() only uses them for float types, and is required for double types. l (letter l) modifier

* Modifier:

        * Provides a distinct service in scanf(), when placed between % and the specifier letter, it causes the function to skip the corresponding input item

Output result:

The return value of scanf():

The scanf() function returns the number of successfully read items. If it does not read any items (such as what happens when it expects a number and you enter a non-numeric character), scanf() returns 0

When it detects the "end of file", it returns EOF (EOF is a special value defined in the file stdio.h. Generally, the #define directive defines the value of EOF as -1)

Output:

printf(), outputs the specified data to the screen according to the user-specified format.

Format control string:

Describe the output format of each output item in the output table, which is divided into format strings and non-format strings . Non-format strings are printed as they are when output; format strings are strings starting with %, followed by "%" followed by different format characters to describe the type, form, length, decimal places, etc. of the output data. The format string has the form: % [output minimum width] [.precision] [length] type

Output table items:

The items to be output are listed, and each output item is separated by a comma. The output table item does not need to be included, which means that the output is the format string itself.

Commonly used output formats and meanings:

Regarding the return value of printf() :

The return value of the rintf function is the number of characters printed. If there is an output error, printf() will return a negative number (some older versions of printf() will have different values).

Character input and output

Input function:

getchar(), receives a character entered by the user from the keyboard

Output function:

putchar(), outputs a single character on the terminal (monitor) 

String input and output

enter:

gets(), receives a string from the keyboard

When the include <stdio.h> header file is included, you can use gets to receive strings directly into the character array.

working process:

When the gets function is executed, the program will wait for the string input by the user from the keyboard. Once the end sign is encountered, that is, press enter, the string previously entered in the buffer will be input together into the memory area pointed to by str in gets. inside. This process is similar to the scanf function. The main difference is that the end mark when scanf receives includes spaces and carriage returns, while gets does not include spaces. This means that gets can receive spaces themselves as part of the content

Notice:

gets does not check the length of the input string, which may exceed the length of the string array and cause memory overflow.

Output:

puts(), prints a string to the console

After including the header file #include <stdio.h>, you can pass the address of the string or character array you want to output into puts to print the output.

Compared with printf(), puts() does not need to specify the string type when outputting, and it will automatically wrap without adding a newline character at the end.

Guess you like

Origin blog.csdn.net/m0_74436212/article/details/131263939