C ++ function subject --sprintf

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/chen1083376511/article/details/91639442

Title: Results The following code execution is how much? 

char buffer[200],s[]="hello",c='c';
int i=35,j;
float fp=1.7320534f;
j=sprintf(buffer,"%s",s);
j+=sprintf(buffer+j,"%c",c);
j+=sprintf(buffer+j,"%d",i);
j+=sprintf(buffer+j,"%f",fp);
printf( "字符个数= %d\n", j );

[ Refer to the answer ]

The number of characters = 16

[A]

As the buffer writing data formatted in a string buffer . That output is helloc351.732053, it is noted that the result does not include '\ 0' , the end character.

Title: Results The following code execution is how much?

char s[20];
sprintf(s,"%d",123);
sprintf(s,"%-4d%-4d",123,456);//左对齐
sprintf(s,"%4d%4d",123,456);//右对齐
printf("%s",s);

[ Refer to the answer ]

 123 456。

Title: Results The following code execution is how much?

char s[20];
sprintf(s,"%8x",4567);//右对齐,小写字母,11d7
sprintf(s,"%-8X",4567);//左对齐,大写字母,11D7
sprintf(s,"%08X",4567);//宽为8个字符,左边补零
printf("%s",s);

[ Refer to the answer ]

000011D7

Title: Results The following code execution is how much?

char s[20];
short si=-1;
sprintf(s,"%04X",si);
printf("%s\n",s);
sprintf(s,"%04X",(unsigned short)si);
printf("%s\n",s);
sprintf(s,"%08X",(unsigned short)si);
printf("%s",s);

[ Refer to the answer ]

FFFFFFFF

FFFF

0000FFFF

[A]

The first print produce "FFFFFFFF", how is it? Because sprintf a variable argument function , in addition to the foregoing two parameters, the latter type of parameter is not safe, and no way function only come by being pressed when a "% X" can know that the original function is called before the pushing of the parameters in the end of the 4-byte integer or an integer number of 2-byte short, so it takes a unified 4 -byte processing mode, when pushing of the parameters leads do sign extension , expanded into a 32-bit integer-1, when printing 4 position is not enough, put the 32-bit integer hex -1 8 are printed out.

Topic: how to turn digital strings (and atoi opposite) ? There itoa function?

[Refer to the answer]

It can be the use sprintf:
sprintf (String, "% D", Number);

(Use sprintf will not have to worry fuss, do not have to worry about wasting run time or code space, in practice it works fine.)

Similarly, it can be converted to a long string or floating-point type (with or% ld% f) with sprintf. In other words, it can be seen as sprintf atol and atof the inverse function. At the same time, you also have some form of control. For these reasons, C sprintf provided as a general solution, rather than itoa.

Title: The following code output is how much? 

char s[20];
sprintf(s,"%f",3.1415926);
printf("%s\n",s);//默认保留小数点为6位数字,3.141593
sprintf(s,"%-10.3f",3.1415926);//"3.142    ",10为宽度,3为3位小数
printf("%s\n",s);
sprintf(s,"%.3f",3.1415926);
printf("%s\n",s);

[ Refer to the answer ]

3.141593

3.142

3.142

Topic: How much output?

int i=100;
sprintf(s,"%.2f",i);
printf("%s\n",s);

[ Refer to the answer ]

0.00 because the caller does not know the parameters onto the stack with i corresponding format control character is "% f". Function is executed while the function itself does not know where the time was pushed onto the stack is an integer, so the poor preservation integer i that four byte is forced without any explanation as to explain the floating-point format, the whole mess.

Title: The following code output is how much?

char a[20];
char *p="Hello,China";
cout<<p<<endl;
_snprintf(a,10,"%s",p);
printf("snprintf a is:%s\n",a);

[Refer to the answer]

Hello,China

snprintf a is: Hello, Chin hot hot hot hot hot hot hot ashamed ??

Analysis: p to a string, when you print p, then put the string printed out. However, if an _snprintf function, to copy a character string which is an array of them, since only the 10 characters ( "hello, Chin") to a copy of the array, the array a remaining 10 characters and not initialized, the output it will be garbled after "hello, Chin".

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/91639442