A brief analysis of the usage and difference of printf of sprintf of scanf of and sscanf of in C language

  • 2020-04-02 01:07:20
  • OfStack

printf
Grammar:
# include < stdio.h >
Int printf(const char *format,... );

The printf() function prints the output to STDOUT(standard output) and other parameters in the format given. The return value is the number of characters printed.
sprintf
Grammar:
# include < stdio.h >
Int sprintf(char *buffer, const char *format... );
The sprintf() function is similar to printf() in that the format control is exactly the same. Any formatted string used by printf can be used in sprintf, but the output is sent to the buffer. The return value is the number of characters written.

Function 1: format numeric string
Sprintf (s, "% - 8 x", 12345); //s becomes: "12345     "
Capital "X" represents, hexadecimal capital form, width takes 8 positions, and "-" represents left alignment.

Function 2: control the format of floating point number printing
Floating point Numbers are controlled by the format character "%f". By default, the format of %m.nf is reserved for 6 digits after the decimal point, where m represents the width of printing and n represents the number of digits after the decimal point
Sprintf (s, "f" % 10.3, 3.1415626); //s becomes:"           3.142"

Function 3: concatenate two strings
Direct connection:
Char dest [256].
Char src1 [] = {' a ', 'b', 'c', 'd', 'e'};
Char src2 [] = {' 1 ', '2', '3', '4'}; s.
Printf (dest, %. 5 s %. "4 s", src1, src2); / / the output: "abcde1234"

Intercepts certain characters of a string for concatenation,
Char dest [256].
Char src1 [] = {' a ', 'b', 'c', 'd', 'e'};
Char src2 [] = {' 1 ', '2', '3', '4'};
Sprintf (dest, "%. * s %. * s", 2, src1, 3, src2); / / the output: "ab123"

Function four: character /Ascii code contrast
We know that if you print a character with "%d" or "%x", you can get its ASCII code in decimal or hexadecimal. Conversely, print an integer with "%c" to see the ASCII character for which it corresponds. The following program segment prints the ASCII code comparison table of all visible characters to the screen (printf is used here, note that the prefix "0X" is automatically added to hexadecimal Numbers when "#" and "%X" are used together) :
For (int I = 32; i. < 127; I++) {
      Printf ("[%c]: %3d 0x%#04X\n", I, I, I);
}

Function 5: print address information
Sometimes when debugging a program, we might want to look at the address of some variable or member. Since the address or pointer is only a 32-bit number, you can print them out with the "%u" of an unsigned integer:
Sprintf (s, "% u", & I);
But usually people prefer to use hexadecimal instead of hexadecimal to display an address:
Sprintf (s, "% x" 08, & I);
However, these are indirect methods. For address printing, sprintf provides a special "%p" :
Sprintf (s, "% p", & I);
I think it's actually equivalent to:
Sprintf (s, "%0*x", 2 * sizeof(void *), & I);

Feature 6: take advantage of the return value
The return value of printf and sprintf is the number of characters written.
That is, every time a sprinf call ends, you don't have to call strlen again to know the length of the resulting string. Such as:
Int len = sprintf(s, "%d", I);
The scanf
Grammar:
  # include < stdio.h >
  Int scanf(const char *format,... );

The scanf() function reads from stdin(standard input) in a format specified by format and saves the data to other parameters.
sscanf
Grammar:
  # include < stdio.h >
  Int sscanf(const char *buffer, const char *format,... );

The function sscanf() is similar to scanf(), except that the input is read from the buffer.
Sscanf is similar to scanf in that it is used for input, except that the latter takes the screen (stdin) as the input source and the former takes a fixed string as the input source
Usage:
%[] indicates that you want to read into a set of characters, and if [the first character after is "^", it means the opposite. The string in [] can be composed of 1 or more characters. An empty character set (%[]) is against the rules and can lead to unpredictable results. %[^] is also against the rules.

%[a-z] reads strings between a-z and stops if not before, e.g
Char s[]="hello, my friend "; // note: the comma is between no a-z
Sscanf (s, "%[a-z]", string); / / string = hello

%[^a-z] reads strings that are not between a-z and stops if a character between a-z is encountered, e.g
Char [] s = "HELLOkitty"; // note: the comma is between no a-z
Sscanf (s, "%[^a-z]", string); / / string = HELLO

The * sign before %*[^=] indicates that the variable is not saved. Skip qualified strings.
Char [] s = = "notepad 1.0.0.1001";
Char szfilename [32] = "";
Int I = sscanf(s, "%*[^=]", szfilename); // szfilename=NULL because it was not saved
Intj = sscanf(s, "%*[^=]=%s", szfilename); / / szfilename = 1.0.0.1001

%40c reads 40 characters
%[^=] reads the string until the '=' sign is encountered. '^' can be followed by more characters, such as:
Char [] s = = "notepad 1.0.0.1001";
Char szfilename [32] = "";
Int I = sscanf(s, "%[^=]", szfilename);                     / / szfilename = notepad
Notepad can also be read from notepad:1.0.0.1001 if the parameter format is: %[^=:]


Related articles: