Printf
Grammar:
#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
Grammar:
#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% #04X \ n", I, I, I);
}
Function Five: Print address information
Sometimes when debugging a program, we might want to look at the address of some variable or member, 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 a dedicated "%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 * Format, ...); 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 * Buffer, const char *format, ...); The
function is similar to SSCANF () and scanf (), except that the input is read from buffer (buffer).
SSCANF is similar to scanf and is used for input, except that the latter takes the screen (stdin) as the input source, the former with a fixed string as the input source
usage:
% [] means to read a character set, if [the latter first character is "^ ", it means" no ". 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: Commas are not between A-Z
sscanf (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