C language: sprintf() usage details

1. Introduction to sprintf()

1. Function prototype

int sprintf(char *string, char *format [,argument,...]);

2. Parameters

  • string – This is a pointer to a character array that stores C strings.
  • format -format string, the structure is the same as printf function: %[flags] [width] [.prec] [length] type .
  • [argument]…-Parameter table, according to different format strings, there are different parameter tables.

3. Function

Write formatted data into a string buffer.

4. Header files

#include<stdio.h>

5. Return value

If successful, it returns the total number of characters written, excluding the null character appended to the end of the string. If it fails, it returns a negative number.
sprintf returns the number of bytes written into string in the result of format argument as the content, and the ending character'\0' is not counted. That is, if "Hello" is written into a string with enough space, the function sprintf returns 5.

Two, sprintf() usage

The function of the sprintf function is basically the same as that of the printf function, except that it outputs the result to the specified string.

1. Print an integer to a string

The specific code is as follows:

#include <stdio.h>

int main(void)
{
    
    
	char sdata[200]={
    
    0};
	
	sprintf(sdata,"%d",520);
	printf("%s\n",sdata);
	
	sprintf(sdata, "%-6d%6d", 520,5231); 		//默认右对齐 
	printf("%s\n",sdata);
	
 	sprintf(sdata, "0x%06x 0X%06X", 520,5231);	//小写和大写16进制 
 	printf("%s\n",sdata);
 	
	return 0;
}

The results are as follows:

520
520     5231
0x00146f 0X00146F

2. Control the printing format of floating point numbers

The specific code is as follows:

#include <stdio.h>

int main(void)
{
    
    
	char sdata[200]={
    
    0};
	
 	sprintf(sdata, "%f", 3.1415926); 			//%f默认保留小数点后6 位
 	printf("%s\n",sdata);
 	
 	sprintf(sdata, "%8.3f", 3.1415626);			//指定宽度,不足的左边补空格:
 	printf("%s\n",sdata);
 	
 	sprintf(sdata, "%.3f", 3.1415626);			//不指定总宽度
 	printf("%s\n",sdata);
 	
	return 0;
}

The results are as follows:

3.141593
   3.142
3.142

3. Connection string

Since you can insert various things into the format control string of sprintf() and finally "connect them into a string", you can naturally connect strings. sprintf() can concatenate multiple strings at once (naturally, you can also insert other content between them at the same time, in short, it is very flexible).
The specific code is as follows:

#include <stdio.h>

int main(void)
{
    
    
	char sdata[200]={
    
    0};
	char* who = "I";
	int m = 100;
	
	sprintf(sdata, "%s love %d 元.", who,m);//简单的字符串拼接 
	printf("%s\n",sdata);
 	
	return 0;
}

The results are as follows:

I love 100.

4. Print address information

The specific code is as follows:

#include <stdio.h>

int main(void)
{
    
    
	char sdata[200]={
    
    0};
	int n=0;
	
	sprintf(sdata,"%u",&n);//十进制地址 
	printf("%s\n",sdata);
	
	sprintf(sdata,"%08X",&n);//十六进制地址 
	printf("%s\n",sdata);	
 
 	sprintf(sdata,"%p",&n);	// 专用打印地址 
	printf("%s\n",sdata);	
	return 0;
}

The results are as follows:

6356556
0060FE4C
0060FE4C

5. Use the return value of sprintf

sprintf() returns the number of characters finally printed to the character buffer in this function call. 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.
The specific code is as follows:

#include <stdio.h>

int main(void)
{
    
    
	char sdata[200]={
    
    0};
	int len=0;
	
	len=sprintf(sdata,"%s!","I love you");
	printf("len=%d\ndata=%s\n",len,sdata);
	
	len+=sprintf(sdata+len,"%d",1314);
	printf("len=%d\ndata=%s\n",len,sdata);
	
	len+=sprintf(sdata+len,"--%p。",&len);
	printf("len=%d\ndata=%s\n",len,sdata);
	return 0;
}

The results are as follows:

len=11
data=I love you!
len=15
data=I love you!1314
len=27
data=I love you!1314--0060FE4C。

3. Common problems with using sprintf()

sprintf() is a variadic function, it often has problems when used, and as long as there is a problem, it is usually a memory access error that can cause the program to crash. Fortunately, the problem caused by the misuse of sprintf is serious, but it is easy to find out. So several situations.

  • Buffer overflow: The assigned length of the first parameter is too short. Try to make it larger. When printing a string, try to specify the maximum number of characters in the form of "%.ns".
  • Forget the first parameter: too low-level problem can not be low-level, using printf too used to.
  • The variable parameter corresponds to a problem: usually it is forgotten to provide the variable parameter corresponding to a certain format symbol, which causes all the subsequent parameters to be misplaced.

Guess you like

Origin blog.csdn.net/MQ0522/article/details/111028283