C language丨printf(), sprintf(), scanf(), sscanf() usage and difference

Today, the usage and differences of printf(), sprintf(), scanf() and sscanf() in C language have been analyzed and introduced in detail. Friends who need it can refer to it.

 

1、printf

grammar: 

#include

int printf( const char *format, ... );

The printf() function prints output to STDOUT (standard output) and other parameters according to the format given by format. The return value is the number of characters output.

2、sprintf

grammar: 

#include

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 it is a format string used by printf, it 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 a string of numbers 

sprintf(s, "%-8X", 12345); //s becomes: "12345"

The uppercase "X" represents the uppercase hexadecimal form, and the width occupies 8 positions, and the "-" represents left-justified.

Function 2: Control the printing format of floating-point numbers 

Floating-point numbers are controlled by the format character "%f", and 6 digits after the decimal point are reserved by default. %m.nf" format, where m represents the width of printing, and n represents the number of digits after the decimal point 

sprintf(s, "%10.3f", 3.1415626); //s变成:"      3.142"

Function 3: 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”

Intercept some characters of the string for connection,

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 4: Character/Ascii code comparison

We know that if you print a character with "%d" or "%x", you can get its decimal or hexadecimal ASCII code; conversely, use "%c" to print an integer and you can see It corresponds to the ASCII character. The following program segment prints the ASCII code comparison table of all visible characters on the screen (printf is used here, note that when "#" and "%X" are used together, the hexadecimal number is automatically prefixed with "0X"):

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 may want to check the addresses of certain variables or members. Since the address or pointer is only a 32-bit number, you can use the "%u" that prints unsigned integers to print them out:

sprintf(s, "%u", &i);

But usually people still like to use hexadecimal instead of decimal to display an address:

sprintf(s, "%08X", &i);

However, these are all indirect methods. For address printing, sprintf provides a special "%p":

sprintf(s, "%p", &i);

I think it is actually equivalent to:

sprintf(s, "%0*x", 2 * sizeof(void *), &i);

Function 6: Use the return value

The return value of printf and sprintf is the number of characters written.

That is to say, after each sprinf call ends, you do not need to call strlen again to know the length of the result string. Such as:

int len = sprintf(s, "%d", i); 

3、scanf

grammar: 

#include

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.

4、sscanf

grammar: 

#include

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, both are used for input, except that the latter uses the screen (stdin) as the input source, and the former uses a fixed string as the input source

usage:

%[] means to read a character set, if the first character after [is "^", it means the opposite. The character string in [] can consist of 1 or more characters. The empty character set (%[]) is against the regulations and can lead to unpredictable results. %[^] is also against the regulations.

%[az] Read the string between az and stop if it is not before, such as

char s[]="hello, my friend"; // Note:, the comma is not between az

sscanf( s, “%[a-z]”, string ) ; // string=hello

%[^az] Read the character string not between az, and stop if it touches the character between az, such as

char s[]="HELLOkitty"; // Note:, the comma is not between az

sscanf( s, “%[^a-z]”, string ) ; // string=HELLO

%*[^=] preceded by * means 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 is not saved

intj = sscanf( s, "%*[^=]=%s", szfilename ) ;// szfilename=1.0.0.1001

%40c read 40 characters

%[^=] Read the string until it hits the'=' sign, there can be more characters after the'^', 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 notepad can also be read from notepad:1.0.0.1001.

The above is a detailed introduction about the usage and differences of printf(), sprintf(), scanf(), and sscanf(). I hope it will be helpful to your study.

 The keyboard is not broken, and the monthly salary is less than 10,000!

If you want to better improve your programming ability, learn C language and C++ programming!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112987647