Printf () function details, printf function details
The most primitive data input and output goes through the terminal (black window). With the development of computer technology, a graphical interface (such as a webpage form) appears) and the development process of commonly used voice input and output (IOS Siri and Android xunfei voice) on mobile terminals.
C language provides a series of data input and output functions, from the printf () function used in the first helloworld program and the scanf () function used to read keyboard input () putchar (), getchar (), and so on.
Printf () function description
Printf is designed to print format, that is, format output. The default output is to the terminal (console), and the redirection symbol ">" can be used to output to the text, as shown in the following application.
/* Use the printf function with redirection> output data to the text @ author tony ittimeline@163.com @ date @ website www.ittimeline.net */void printf_text () {printf ("case of outputting data to text"); printf ("architecture design, AI ");}
After you use the VisualStudio menu to generate a solution (Ctrl + Shift + B), in the solution's Debug directory (for example, C: \ Users \ tony \ source \ repos \ CPrimerPlus6 \ debug.pdf there is a compiled binary file cprimerplus6.exe,
You can also format the data to be displayed on a webpage (the 1.6 helloworld chapter explains in detail how to output the data on a webpage, so I will not repeat it here ).
The printf () function parses the data according to the specified format and outputs it without converting the data type. As shown in the following figure, parsing the data in the correct format will produce the correct result.
# Include
/* The printf function parses the data case according to the specified format @ author tony ittimeline@163.com @ date @ website www.ittimeline.net */void prinf_summary () {printf ("hello printf \ n "); // The data output by default to the console (command prompt program) is eventually a string printf ("num = % d \ t str = % s \ n", 10, "timeless"); // output to the command line terminal, file, webpage system ("pause ");}
C language does not provide Java to connect strings through the plus sign, but can use the sprintf () function to achieve String concatenation, as shown in the following application, according to the user input string (0-9, A-F ), then the terminal window color is changed by splicing the command and executed on the terminal.
# Define _ CRT_SECURE_NO_WARNINGS # include
# Include
/* Sprintf function for string integration @ author tony ittimeline@163.com @ date @ website www.ittimeline.net */void sprintf_sample () {printf ("enter a string to change the terminal color \ n"); char str [100] = {0}; char input [100] = {0 }; // store the input string scanf ("% s", input); // read the input string //. 2 s is to extract the sprintf (str, "color %. 2 s ", input); // integrate the input string (the second parameter) into the str string system (str ); // execute the command to call system ("pause ");}
Use of common printf format control characters
In daily development, for example, the cgi web page program may need to adjust the data alignment, specify the data width, and intercept data, this will use some common format control characters. The following table describes the specific formatting strings and their functions.
Format character |
Function Description |
- |
The default data output is right-aligned. Here we use the left-aligned method. |
M |
Controls the display width of data. If the actual width is greater than m, it is displayed according to the actual width. If the actual width is less than m, it is filled with 0 or space. |
. N |
If it is used for floating point numbers, it is to retain the n digits after the decimal point. If it is used for characters, it is the length of the truncated characters. |
0 |
Fill the output space with 0 |
L |
Output long integer |
Printf () function General Data Format control character application case is as follows:
# Include
# Include
/* General Format control string detailed description m limited width, if the actual width is redundant m, output according to the actual number of digits, if less than m, then fill zero or space. n for floating point numbers, retain n digits after the decimal point. For strings, truncate n-bit l output long integer o output space fill with 0-output numbers or characters are left aligned (right aligned by default) right fill space @ author tony ittimeline@163.com @ date @ website www.ittimeline.net */void print_format_info () {// int num = 10; printf ("% d \ n", num ); // by default, the right alignment has multiple width distributions and multiple width printf ("% 10d \ n", num); // The width is limited to 10. If the actual number of digits is greater than 10, the output is based on the actual number of digits, if the number is less than 10 digits, add zero or space printf ("% 010d \ n", num); // The width is 10, and the free space is filled with 0 printf ("%-d \ n ", num); // "-" alignment on the left // retain two printf ("%. 2f \ n ", 3.1415927); // capture the string char str [100] =" notepad1 "; char command [100] = {0}; sprintf (command," %. 7 s ", str); system (command); printf (" % ld \ n ", 123456789 ); // The Systems with % d and % ld above 32 bits are equivalent systems ("pause ");}
String truncation case
In addition to the most commonly used string Truncation in daily development, the string truncation function also captures specified content in the middle, such as the date of birth when an ID card is obtained.
The following is an application that intercepts the ID card's birthdate, month, and day.
# Include
# Include
/* Capture ID Card date of Birth @ author ittimeline@163.com @ date 2018/1/1 20:56 @ website www.ittimeline.net */void spintf_split_id_brithday () {// ID card char id_str [20] = "140211197605010352 "; char brithday [10] = {0}; sprintf (brithday, "%. 8 s ", id_str + 6); printf (" current user's birthday is % s ", brithday); system (" pause ");}
# Include
# Include
/* Reverse truncation string with mobile character address @ author ittimeline@163.com @ date 2018/1/1 20:56 @ website www.ittimeline.net */void sprintf_split () {// capture char taskstr1 [100] = "task" from left to right; char liststr1 [100] = "list123"; char taskliststr1 [100] = {0 }; sprintf (taskliststr1, "% s %. 4 s ", taskstr1, liststr1); // The four characters printf (" taskliststr1 = % s ", taskliststr1) are intercepted from left to right; system (taskliststr1 ); // intercept the char taskstr2 [100] = "task"; char liststr2 [100] = "123list123"; char taskliststr2 [100] = {0}; sprintf (taskliststr2, "% s %. 4 s ", taskstr2, liststr2 + 3); // liststr truncates four characters in length from left to right, this character address is moved three characters backward (this will discard the 123 characters on the left) printf ("taskliststr2 = % s", taskliststr2); system (taskliststr2 ); system ("pause ");}
In daily development, the most common use scenario of printf () is to parse various data types, such as integers, floating point numbers, characters, strings, and so on.
The commonly used formats for integer parsing by printf () functions are as follows:
The integers are divided into octal, decimal, and hexadecimal. The default value is decimal.
A decimal integer is divided into signed and unsigned integers by or without symbols.
Format a string |
Resolution Method |
% D/% I |
Signed decimal integer |
% U |
Unsigned decimal integer |
% O |
No octal integer |
% X |
Unsigned hexadecimal integer |
% Lld |
Signed long type |
The following is a summary case of printf's integer parsing:
# Include
# Include
/* Printf parse integer Summary % d % I signed decimal % u unsigned integer % o unsigned octal % x unsigned hexadecimal @ author tony ittimeline@163.com @ date 2018/1/1 @ website www.ittimeline.net */void printf_int () {int num = 100; // a decimal integer can only distinguish printf with or without symbols ("the signed decimal value of 100 is % d \ n", 100 ); // The output result shows that % d and % I have the same display effect as printf ("the signed decimal value of 100 is % I \ n", 100 ); printf ("100 in unsigned decimal format: % u \ n", 100); printf ("100 in unsigned octal notation: % o \ n", 100 ); printf ("100 of the unsigned hexadecimal tabulation value is % x \ n", 100); // in T and long are equivalent and occupy 4 bytes of memory. // However, on a 16-bit machine, short and int are equivalent and both occupy the size of two bytes. // Thus, different machines are distinguished, % ld is recommended for parsing long data to obtain the correct resolution result long lngVal = 12345678910; printf ("lngVal = % d \ t lngVal = % ld", lngVal, lngVal); system ("pause ");}
The formats of characters and strings parsed by the printf () function are as follows:
Format String |
Resolution Method |
% C |
Character |
% S |
String |
The application cases of the printf () function to parse characters and strings are as follows:
You can also use the putchar () function to output a single character.
# Include
# Include
/* Printf function handles characters and strings % c output characters % s output string @ author tony ittimeline@163.com @ date 2018/1/1 20:12 @ site www.ittimeline.net */void printf_char_str () {char ch = 'a'; printf ("the ch variable stores the characters % c \ n", ch); // use putchar () the function outputs a character putchar (ch); char cmd [100] = "notepad"; printf ("% s", cmd); system ("pause ");}
The format of the floating point number parsed by the printf () function is shown in the following table.
Format String |
Resolution Method |
% F |
Floating Point Number |
% E |
Exponent (Scientific notation, generally used to represent astronomical numbers) |
% G |
Select the representation with the smallest width between % f and % e. |
Application Case of printf () function output floating point number
# Include
# Include
/* Printf () function output floating point data @ author tony ittimeline@163.com @ date 2018/1/1 21:30 @ site www.ittimeline.net */void printf_float_double () {double dblVal = 12.3456789; printf ("the result of taking 4 digits after decimal point of Data 12.3456789 is %. 4f \ n ", dblVal); // extremely large data and extremely small data in daily development are usually expressed as double chinese_gdp = 80000000000000; printf ("China's GDP this year is % e \ n", chinese_gdp ); printf ****************** ** \ n "); // % g automatically selects the data format storage with a small width (% f or % e), which can save memory // double db = 1234567890000; double db = 1234.56789; // % f is reserved by default after the decimal point. printf ("% f \ n, % e \ n, % g \ n", db, db, db); // g automatically selects one of the shorter width (% f | % e) for printing system ("pause ");}