![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
printf or cout?
ive seen alot of c++ examples using printf over cout. Can someone please tell me the difference between the two? and, if there are differences what are the advantages and disadvantages of printf?
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
The main difference is that printf is the C-way of printing formatted text and numbers, cout is the object-oriented, C++-way of printing formatted text.
Another difference is that cout is usually safer, as you can make the following mistake with printf: printf("%i %i", 5); |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
should i bother to learn to use printf?? it looks complicated cause of all of the % and stuff.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just depends on what you call complicated, I guess. Have you tried to format cout with iomanip, et.al.? Do you ever plan to use C?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
no i dont think i plan to ever use C.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
I know of no reason to use printf when programming in C++ (but I'm not an expert, if anyone knows a reason I'd like to hear it).
But I think it's a good idea to learn the basics of printf (and C syntax in general) anyway. Even if you plan to program in C++, you never know when knowledge of C will prove useful. Perhaps one day you'd like to understand an algorithm you can only find a C version of, or something. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Well I can't think of a reason either, fprintf might prove useful in some situations, but one can often use stringstreams, which in my opinion is a lot more graceful.
It doesn't hurt to know how printf works. I would not use it in C++ if I can avoid it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Dec 2005
Location: Philippines, where the seasons are: hot, and hotter
Posts: 72
Rep Power: 3
![]() |
I still use printf, even in c++ environment, simply because I started programming in C, and it's kinda hard to stray from what you've learned, at least in my case. You can still use printf in the c++ environment by using stdio.h as a header file.
__________________
"The most incomprehensible idea about the universe is that it is comprehensible" - Albert Einstein |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
@alphonso: You should include <cstdio>, stdio.h is deprecated in C++.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Dec 2005
Location: Philippines, where the seasons are: hot, and hotter
Posts: 72
Rep Power: 3
![]() |
Ok, I'll do that. Thanks polyphemus
![]()
__________________
"The most incomprehensible idea about the universe is that it is comprehensible" - Albert Einstein |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|