c++ - 我应该在我的 C++ 代码中使用 printf 吗?

标签 c++ formatting

我一般使用 coutcerr 将文本写入控制台。但是有时我发现使用旧的 printf 语句更容易。当我需要格式化输出时使用它。

我会使用它的一个例子是:

// 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。我有什么理由不应该使用 printf 语句?

最佳答案

我的学生,他们先学习 cincout,然后再学习 printf,绝大多数人更喜欢 printf (或更常见的是 fprintf)。我自己发现 printf 模型具有足够的可读性,因此我已将其移植到其他编程语言中。 Olivier Danvy 也是如此,谁甚至使它成为类型安全的。

如果您有一个能够对 printf 调用进行类型检查的编译器,我认为没有理由不在 C++ 中使用 fprintf 和 friend 。

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

关于c++ - 我应该在我的 C++ 代码中使用 printf 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2017489/

相关文章:

c++ - 如何将 unique_ptr 的 vector 传递给函数

python - 没有加号的科学记数法

c++ - 如何在 Visual Studio C++ 中打印整个表格?

c++ - Python 和 win32 API - 使用 C 文件

c++ - 使用静态变量的递归调用的不同输出

java - String.format 或类似的正确格式字符串

haskell - 将 NominalDiffTime 打印为小时、分钟和秒

android - PhoneNumberFormattingTextWatcher 不工作

r - 通过将空格填充到指定长度来使字符串居中

c++ - atomic_thread_fence 不是 std 的成员