动态内存的申请与释放必须配对,防止内存泄漏。
1 #include <iostream> 2 #include<stdio.h> 3 #include <string.h> 4 5 //main()函数 6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 7 using namespace std; 8 int main(int argc, char** argv) { 9 //声明变量和数组 10 char buffer[200], s[] = "computer", c = 'l'; 11 int i = 35, j; 12 float fp = 1.7320534f; 13 14 //格式化输出到buffer 15 j = sprintf( buffer, " String: %s ", s ); 16 j += sprintf( buffer + j, " Character: %c ", c ); 17 j += sprintf( buffer + j, " Integer: %d ", i ); 18 j += sprintf( buffer + j, " Real: %f ", fp ); 19 20 cout<<"Output:"<<endl; 21 cout<<buffer; 22 cout<<"character count ="<<j<<endl; 23 return 0; 24 }