The following is a detailed analysis of the use and distinction of printf (), sprintf (), scanf (), sscanf () in the C language, and the need for friends can refer to the following
Printf
Syntax:
#include <stdio.h>
int printf (const char *format, ...);
The printf () function prints output to stdout (standard output) and other parameters according to the format given in format. The return value is the number of characters that are output.
sprintf
Syntax:
#include <stdio.h>
int sprintf (char *buffer, const char *format, ...);
The sprintf () function is similar to printf (), and the format control is exactly the same, as long as the formatted string used by printf is available in sprintf and only sends the output to buffer (buffer). The return value is the number of characters written.
Function Number one: Format a string of numbers
sprintf (S, "%-8x", 12345); S into: "12345"
The uppercase "X" stands for 16 in uppercase, with a width of 8 positions, and "-" to represent left-aligned.
Function Two: control floating-point printing format
floating-point numbers use the format character "%f" control by default, the 6-digit%m.nf after the decimal point is retained, where m represents the width of the print, and n indicates the number of digits after the decimal point
sprintf (S, "%10.3f", 3.1415626); S into: "3.142"
Function Three: Connect two strings
Direct connection:
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};s
printf (dest, "%.5s%.4s", SRC1,SRC2); Output: "abcde1234"
Intercepts some characters of a string to connect,
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};
sprintf (dest, "%.*s%.*s", 2,SRC1,3,SRC2); Output: "ab123"
Function Four: Character/ASCII code control
we know that using "%d" or "%x" to print a character, you can get its 10-or 16-in ASCII, which, in turn, prints an integer with "%c" to see its corresponding ASCII character. The following section prints the ASCII-code table of all visible characters to the screen (printf here), noting that "#" when combined with "%x" automatically adds a "0X" prefix for the 16 number:
for (int i = i < 127; i++) {
printf ("[%c]:%3d 0x% #04Xn", I, I, I);
}
feature Five: print address information
Sometimes when debugging a program, we might want to look at some variables or members ' addresses, because the address or pointer is just a 32-digit number, you can print them out using "%u" that prints unsigned integers:
sprintf (S, "%u", &i);
But usually people prefer to use 16 instead of 10 to display an address:
sprintf (S, "%08x", &i);
However, these are indirect methods, for address printing, sprintf provides specialized "%p":
sprintf (S, "%p", &i);
I think it's actually equivalent to:
sprintf (S, "%0*x", 2 * sizeof (void *), &i);
feature Six: Use the return value
The return value of printf and sprintf is the number of characters written.
that is, every time a sprinf call is completed, you do not have to call again strlen to know the length of the resulting string. such as:
int len = sprintf (S, "%d", I);
scanf
Syntax:
#include <stdio.h>
int scanf (const char *FO Rmat, ...); The
scanf () function reads from stdin (standard input) according to the format specified by format (format) and saves the data to other parameters.
sscanf
Syntax:
#include <stdio.h>
int sscanf (const char *buff Er, const char *format, ...); The
function is similar to SSCANF () and scanf (), except that the input is read from the buffer (buffer).
Sscanf is similar to scanf for input, except for the input source with a fixed string (stdin)
usage:
% [] means to read a set of characters, if the first character is "^", then the inverse meaning. The string within [] can be composed of 1 or more characters. The null character set (%[]) is a violation of the rules and can lead to unpredictable results. %[^] is also a violation of regulations.
%[a-z] reads a string between A-Z, if not before stopping, such as
char s[]= "Hello, my Friend";//Note: Comma is not a-Z between
SSC ANF (S, "%[a-z]", string); String=hello
%[^a-z] reads a string that is not A-Z, and stops if the character between A-Z is encountered, as
Char s[]= "Hellokitty"; Note: The comma is not between A-Z
SSCANF (S, "%[^a-z]", string); String=hello
%*[^=] preceded by a * number indicates that the variable is not saved. Skip strings that match the criteria.
Char s[]= "notepad=1.0.0.1001";
Char szFileName [32] = "";
int i = sscanf (S, "%*[^=]", szfilename);//Szfilename=null, as not saved
INTJ = sscanf (S, "%*[^=]=%s", szfilename);//szfilename=1.0.0.1001
%40C Read 40 characters
%[^=] reads the string until it touches the ' = ' sign, ' ^ ' can take more characters, such as:
Char s[]= "notepad=1.0.0.1001";
Char szFileName [32] = "";
int i = sscanf (S, "%[^=]", szFileName); Szfilename=notepad
If the parameter format is:%[^=:], then you can also read Notepad from notepad:1.0.0.1001