Why iostream and not stdio?

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I am curious as to why people eschew printf/sprintf in favor of cout, ios::, etc. Printf is probably the best function in all of the libraries as it offers so much flexibility and power with so little code. For example, if I wanted to put the current time in the format "00:00:00" (hh:mm:ss):

time_t curr;
tm local;
time(&curr);
local=*(localtime(&curr)); // dereference and assign

With cout

cout.width(2); cout.fill('0');
cout << local.tm_hour << ":";
cout.width(2); cout.fill('0');
cout << local.tm_min << ":";
cout.width(2); cout.fill('0');
cout << local.tm_sec << " ";
cout.width(0);

Why all that work when you can just do

With printf

printf("%02d:%02d:%02d", local.tm_hour, local.tm_min, local.tm_sec);

(Yes, I have seen several examples of people doing it the hard way.)
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I would assume mostly because printf is part of the standard C library and iostream is part of the standard C++ libraries. A lot of people really hate reading through code that's a mix of the two.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
I've mostly used iostream, since my C++ class I've had this semester only touched on C for a few days. But differences we learned was that iostream was type safe, object-oriented, and could make use of C++ features like operator overloading. Overloading << or >> is pretty handy for easily printing user made objects. Though your example is a good one why printf can be more useful.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,002
126
Yeah, type safing is a big reason. Specifically if you pass the wrong type parameters to printf it can make very difficult bugs to track down.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Trying to work with functions that have variable numbers of arguments makes me want to either cry or puke.

It's *much* easier to overload and write wrappers around the iostream stuff than printf(). For instance, you can have a function that takes a pointer to an arbitrary iostream and then prints to that. fprintf() sort of does this, but can only print to a (FILE*).

I will admit that I have, at times, used sprintf() to write a formatted string and then outputted it using an iostream. When you want really complex formatting, it's usually easier to understand the printf() formatting string than dozens of cout commands.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |