我应该用printf在我的C ++ code? [英] Should I use printf in my C++ code?

查看:93
本文介绍了我应该用printf在我的C ++ code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一般使用 COUT CERR 写入文本控制台。不过有时我觉得它更容易使用好老的printf 语句。我用它当我需要格式化输出。

So I generally use cout and cerr to write text to the console. However sometimes I find it easier to use the good old printf statement. I use it when I need to format the output.

所以,在那里我会用这一个例子是:

So one example of where I would use this is:

// Lets assume that I'm printing coordinates... 
printf("(%d,%d)\n", x, y);

// To do the same thing as above using cout....
cout << "(" << x << "," << y << ")" << endl;

我知道我可以使用 COUT 格式的输出,但我已经知道如何使用的printf 。那么,有什么理由我不应该使用C 的printf 语句?

I know I can format output using cout but I already know how to use the printf. So is there any reason I shouldn't use the C printf statement?

推荐答案

我的学生,谁学习 CIN COUT 第一,然后学习的printf 后,绝大多数preFER 的printf (或者更通常 fprintf中)。我自己已经找到了的printf 模式充分可读性,我已经将它移植到其他编程语言。所以有奥利维尔Danvy ,谁也连上了类型安全的。

My students, who learn cin and cout first, then learn printf later, overwhelmingly prefer printf (or more usually fprintf). I myself have found the printf model sufficiently readable that I have ported it to other programming languages. So has Olivier Danvy, who has even made it type-safe.

只要你有一个编译器,它能够类型检查调用的printf ,我看不出有什么理由不使用 fprintf中和朋友在C ++中。

Provided you have a compiler that is capable of type-checking calls to printf, I see no reason not to use fprintf and friends in C++.

声明:我是一个糟糕的C ++程序员

Disclaimer: I am a terrible C++ programmer.

这篇关于我应该用printf在我的C ++ code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆