printf怎么用cout替代
对非格式化输出,直接替换就可以,如
例一:
printf("ok");
可改为
cout << "ok" ;
例二:
int x=10;
printf("x=%d ",x);
改为
int x=10;
cout << "x=" << x << endl;
对格式化的输出 ,要繁些
例三:
double x=12.35;
printf("%08.4f ",x);
要改为
double x=12.35;
cout << setw(8) << setfill('0') << fixed << setprecision(4) << x << endl;
/////////////////////
C++的cout是最基本的输出指令,必须要熟悉的,但C是C++的子集,所以在c++中,在需要格式化输出的地方,完全可不用cout而还用printf
上一篇:怎么有效的学单片机
热门标签: