1. printf FunctionThe printf function is a library function that is already implemented in the C language, but it requires the inclusion of the header file: include <stdio.h>The printf function allows you to specify placeholders in the output text. A placeholder is a position that can be replaced with other values, which has been discussed in previous articles.The first character of a placeholder is always a percent sign (%), and the second character indicates the type of the placeholder. The second parameter of the printf function is the value that replaces the placeholder, and the order of the placeholders corresponds one-to-one with the values. As shown in the figure below:
Specifying Minimum Width:The printf function allows you to specify the minimum width of a placeholder. For example, %5d indicates that the width of this placeholder is at least 5 digits. If it is less than 5 digits, spaces will be added in front of the corresponding value. If it exceeds 5 digits, it will display as many digits as it has, and it is left-aligned by default. If there is a decimal point, the width includes the decimal point.Specifying Decimal Places:When outputting decimals, you may want to limit the number of decimal places. For example, if you want to keep two decimal places, the placeholder can be written as %.2, and it will be padded with zeros if necessary.
2. scanf FunctionThe scanf function is mainly used to get data from the keyboard into variables in the program. It is also a library function and requires the same header file as the printf function. The syntax is as follows:scanf("%d", &score);This means to get a data value from the keyboard and write it into score. The & symbol is the address operator. The scanf function can read multiple variables at once, and the order also corresponds one-to-one.scanf("%d%d%f%f", &a, &b, &c, &d);
If there is a problem with Chinese output garbled during this process, you can refer to【Complete Solution to VS Code Chinese Garbled Problem – Bilibili】 https://b23.tv/4LE0Q8xMeaning of scanf Function Return Value:Normal case: returns the number of successfully read variables;No data read: if no items are read or if matching fails, it returns 0;End of file: if a read error occurs or the end of the file is reached before any data is successfully read, it returns the constant EOF (-1), which means end of file.Finally, here are some common placeholders: