#include<stdio.h>
main()
{
int i=100;
char temp[]="value of i is **** and I can write int inside a string";
printf("\n%s\n",temp);
}
Run Code Online (Sandbox Code Playgroud)
我需要i在字符串中打印值.这样我就可以得到输出:
value of i is 100 and I can write int inside a string
Run Code Online (Sandbox Code Playgroud)
我应该在****的地方写些什么,或者如何更改此代码以获得上述输出?我不想printf用来打印这个输出.
您可以使用sprintf将字符串"打印"到char数组中,就像将其printf打印到屏幕上一样:
char temp[256];
sprintf(temp, "value of i is %d and I can write int inside a string", i);
Run Code Online (Sandbox Code Playgroud)
请注意,您需要确保缓冲区足够大!或者用于snprintf指定最大字符串/文本长度,因此不要在缓冲区外写入.