printf()和cout的区别

printf()和cout的区别
printf is a function that takes a variable number of arguments, the first argument being a format string explaining how to treat further arguments. cout is an object of the std::ostream class. The two are completely different in form, yet the result is the same, data sent to stdout.

This question is difficult to answer completely as often phrased since cout and printf are wildly different, but most often one of three questions are meant:


Q: "Which is faster?" A: printf, but the difference is too slight to worry about.

Q: "Which is better?" A: Neither, it depends heavily on personal preference and what needs to be done.

Q: "Which should I use?" A: You should use whichever you feel most comfortable with.

It is preferred that you use iostreams with C++ instead of the C functions because iostreams are type safe and extensible for user defined types, both of which are very good reasons.

printf是个函数,输出既是一个函数调用;
cout是个变量,一个对象,和" < <"操作符结合使用;输出既是一个表达式;

当你用 cout 输出的时候,系统并不是将其送入屏幕,而是转存到缓冲区,待缓冲区满时一次性显示
解决的办法是,调用一次 cout < < flush;
C语言中为 fflush(stdout);
还有就是 cout < < "/n"; 和 cout < < endl; 的区别
前者仅仅只将回车符送入输出缓存,而后者多一个操作,即立即显示

你可能感兴趣的:(printf)