Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2006, 7:51 AM   #1
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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?
d_heyzie is offline   Reply With Quote
Old Apr 2nd, 2006, 8:09 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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);
Which can output weird stuff, or crash your program (or both).
Polyphemus_ is offline   Reply With Quote
Old Apr 2nd, 2006, 8:16 AM   #3
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
should i bother to learn to use printf?? it looks complicated cause of all of the % and stuff.
d_heyzie is offline   Reply With Quote
Old Apr 2nd, 2006, 8:37 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 2nd, 2006, 8:39 AM   #5
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
no i dont think i plan to ever use C.
d_heyzie is offline   Reply With Quote
Old Apr 2nd, 2006, 9:20 AM   #6
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
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.
Xyhm is offline   Reply With Quote
Old Apr 2nd, 2006, 10:15 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Apr 2nd, 2006, 10:35 AM   #8
alphonso
Programmer
 
Join Date: Dec 2005
Location: Philippines, where the seasons are: hot, and hotter
Posts: 72
Rep Power: 3 alphonso is on a distinguished road
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
alphonso is offline   Reply With Quote
Old Apr 2nd, 2006, 10:41 AM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
@alphonso: You should include <cstdio>, stdio.h is deprecated in C++.
Polyphemus_ is offline   Reply With Quote
Old Apr 2nd, 2006, 10:54 AM   #10
alphonso
Programmer
 
Join Date: Dec 2005
Location: Philippines, where the seasons are: hot, and hotter
Posts: 72
Rep Power: 3 alphonso is on a distinguished road
Ok, I'll do that. Thanks polyphemus
__________________
"The most incomprehensible idea about the universe is that it is comprehensible" - Albert Einstein
alphonso is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC