Are the C formatted I/O functions (printf, sprintf

2020-06-01 04:16发布

I've been looking through a lot of code made by others lately and happened to notice everyone uses "printf" style C functions a lot, but the C++ functions learned in school (cout, specifically) don't seem so popular.

Is this a valid observation, and is there a reason for this? Convention?

Thanks,

R

6条回答
做个烂人
2楼-- · 2020-06-01 04:25

There are several criticisms of the standard stream system -- most notably that it usually is not as performant as C's system, and that they don't allow reordering of the items to be formatted, which can make localization more difficult.

Personally, I use streams for most everything, because they allow me to have a function write to a console, a file, or a string, without having to modify the function.

查看更多
Summer. ? 凉城
3楼-- · 2020-06-01 04:26

I'm going to guess printf is more widely used because

  • it was in use for quite a few years before C++ compilers and streams showed up
  • C is used more than C++
  • lots of I/O was done for things like the Windows API, for which printf is a natural fit amongst Open/Read/Write/Close/etc
查看更多
等我变得足够好
4楼-- · 2020-06-01 04:32

I think taste is one possible reason. Personally I find this:

printf("%8d: %s\n", customer->id, customer->name);

more readable than this:

std::cout << customer->id << ": " << customer->name << std::endl;

There's also the issue with localization. printf makes it possible to change the formatting to suit other languages and UI cultures, which becomes a major chore with iostreams, unless you use something like the Boost Format library.

查看更多
\"骚年 ilove
5楼-- · 2020-06-01 04:34

The printf and scanf family functions have two major problems: type safety and memory safety. It's fairly easy to make a mismatch between the specification string and the variable length argument list that follows. Also, buffer overruns through scanf are a classic security vulnerability. In short, don't use them.

The C++ streams offer type and memory safety, and also formatting extensibility. They're much more powerful and generally easier to use than printf and scanf.

Also, as suggested by ShaderOp, Boost's format library provides the same safety, but makes the old C programmers feel more comfortable.

查看更多
家丑人穷心不美
6楼-- · 2020-06-01 04:37

Where I work, we use printf-style formatting. This is because we used to make heavy use of the MFC CString class and its printf-style Format method. We've been phasing out MFC, but haven't changed our string-formatting approach.

As for which one is designed better, see Who architected / designed C++'s IOStreams, and would it still be considered well-designed by today's standards?

查看更多
Fickle 薄情
7楼-- · 2020-06-01 04:46

Personally, I use printf over the iostream stuff (like cout) because I think it's clearer.

When you do formatting with iostream, you have to << all sorts of weirdness like setiosflags and setf. I can never remember which namespace all this stuff lives in, let alone what it all does. Even when I do, I'm disappointed with how verbose and unintuitive the code looks.

The formatting options with printf may seem illegible at first, but they're concise, clearly documented in a single manual page, and common to a wide range of languages.

Another advanage is that printf is stateless: Unlike with cout, I don't need to remember which member functions have been called on printf, or which byzantine concoction of flags has been <<'ed into it. This is a big plus for readability.

查看更多
登录 后发表回答