sprintf函数的功能与printf函数的功能基本一样,只是它把结果输出到指定的字符串中了:
#include<stdio.h> int main(int argc, char *avgv[]) { char s[40]; sprintf(s,"%s%d%c","test",1,'2'); /*第一个参数就是指向要写入的那个字符串的指针,剩下的就和printf()一样了*/ // printf("%s%d%c","test",1,'2'); //test12 printf("%s",s); //test12 return 0; }