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; 的区别
前者仅仅只将回车符送入输出缓存,而后者多一个操作,即立即显示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值