I'm relatively new to c and c++. Generally I use the g++ compiler but I haven't really been all that careful about using c code as opposed to c++. The way I see it, it shouldn't matter as long as memory is managed. please correct me if I'm wrong.
Anyway, I don't really like the cout function, I prefer printf because I think it is easier to work with. I've been using printf in my c++ code.
One problem I have run into is that I'm not sure how to represent certain variable types such as an unsigned long. %d doesn't work. Is it possible, or is this one of the reasons why I should stick with cout?
The big problems with printf are:
- Can't define your own type (iostream can overload new << operators... printf can't overload new % types).
- it's not type aware and therefore is error prone. Examples:
1 2 3 4 5 6 7 8
printf("%f", 5); // explode
printf("%d", 2.1); // explode
constchar* foo = "I want a 3%discount";
printf(foo); // explode
const std::string bar = "blah blah";
printf("%s", bar) // explode
The above are all incorrect uses of printf and will cause very strange/bad behavior in a program, and will often not trigger any compiler error. So you won't find out there's a problem until your program starts acting stupid.
The big problems with iostream/cout:
- Operates as a state machine which means it can't be threadsafe.
- Extremely verbose (typically much more to type than the printf alternative)
So there's definitely a tradeoff. I tend to prefer C++ solutions (so I'd prefer iostream in this case) for the increased type safety and expandability.
If std::ios_base::sync_with_stdio(false) has not been invoked:
A standard C stream and the corresponding standard C++ stream use the same buffer; input/output with these (for instance printf() and std::cout << ) can be mixed freely; C++ standard streams are guaranteed by the IS to be as thread-safe as the C standard streams (no data races, but interleaved characters may occur).
printf("%u + %u = %u", a, b , c); is nothing, how about printf("%" PRIu32 " + %"PRIu32 " = %"PRIu32, a, b, c);? C I/O can be terribly verbose.
how to represent certain variable types such as an unsigned long
printf cannot represent a variable as any type other than its own, exact, type (barring ellipsis conversions, which are the obscure reason why %f is used for both float and double): if you wrote %lu, the type of the corresponding argument must be unsignedlongint, or else the behavior is undefined.