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
Personally, I use
printfover theiostreamstuff (likecout) because I think it’s clearer.When you do formatting with
iostream, you have to<<all sorts of weirdness likesetiosflagsandsetf. 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
printfmay 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
printfis stateless: Unlike withcout, I don’t need to remember which member functions have been called onprintf, or which byzantine concoction of flags has been<<‘ed into it. This is a big plus for readability.