Click to See Complete Forum and Search --> : printf()
mandreko
03-21-2001, 09:08 AM
i'm trying to use printf() instead of cout in my programming class, because i was told you can have more control... yet when i put it in, i get undefined variable. What is the header file i need for the printf command? i have <iostream.h> but that's about it..
EscapeCharacter
03-21-2001, 09:27 AM
hey finally i question i can answer :) you will need to include stdio.h also if you want to use printf instead of cout.
Energon
03-21-2001, 09:40 AM
How do you get more control from printf()? cout gives you the same level of formatability and then some with the use of other functions within it...
EscapeCharacter
03-21-2001, 09:59 AM
well you can do precision stuff with c++ too i just takes longer to type, guess c programmers are too lazy :P j/k no flames please. to be honest i dont know.
TheLinuxDuck?
Strike
03-21-2001, 10:56 AM
cout gives you more options, printf is just more succinct
mandreko
03-21-2001, 02:59 PM
Originally posted by Strike:
cout gives you more options, printf is just more succinct
ok, what? i was told that printf() had more options and stuff... was my teacher wrong? (This wouldn't be a first time) Generally, what should i use? I'm more familiar with the cout statement, as that's what our books teach us, but I see that people use the printf() in PHP, which i'm also planning to learn soon.
Won't learning C++ help me with javascript and PHP a bit too?
also, does anyone know what the difference between C and C++ are? if i can do C++ can i do C?
Stuka
03-21-2001, 04:27 PM
<soapbox>If you're coding in C++, use cout. It is a part of the standard library for C++, and can be used (via operator overloading) to output any class you create. There's nothing inherently WRONG w/printf(), and knowing its syntax is quite useful, as the printf() functions exist in several languages (PHP, C, perl, probably others), while cout is, of course, C++ specific. Both Javascript and PHP allow OO programming (though I'm not sure that either require it), so knowing OO concepts from C++ will help, and from what I've seen, PHP syntax is similar to C/C++, so that can't hurt. The differences between C and C++ are numerous, some small, some large. Knowing one does help learn the other, although the basic paradigm of programming in C (procedural) is quite different from the OO design paradigm of C++.</soapbox> OK, enough ranting....
mandreko
03-21-2001, 04:49 PM
thanks, that pretty much cleared things up... now i just gotta find out how to use printf, and cout better
Energon
03-21-2001, 04:53 PM
The printf in PHP can probably best though of as coming from Perl and not C (which of course, did come from C, but that's not the point)... much like the generic print function came from Perl (the f just means to format it)... and I would imagine the reason it doesn't have a cout is because that's a very "heavyweight" C++ type of function that probably wouldn't fit at all with the way PHP is put together on the base level...
Stuka
03-21-2001, 07:33 PM
Energon-
I expected more from you! :) cout isn't a function, it's an ostream object! That's why it isn't in PHP - although I discovered just recently that PHP does support classes (way cool IMO). But yeah, cout takes a lot of OO type build-up, and since PHP doesn't really need to do stream I/O, what's the point, right?
Energon
03-21-2001, 10:52 PM
that's true, buuut... the way it's used is through an overloaded operator attached to a function... :)
classes in PHP are pretty cool... I find them to be a lot more like Java classes (since I don't know much about OO Perl) than C++ classes... just something about the way they end up being written that feels like Java to me...
WilliamWallace
03-22-2001, 12:47 AM
Confucius say: it is unwise to mix c and c++...it may confuse short-tempered programmers.
Energon
03-22-2001, 01:10 AM
Energon say: C++ contains every bit of functionality that C does... therefore mixing them is not only wise, but almost unavoidable at some point...
:D
Stuka
03-22-2001, 10:34 AM
Actually, William Wallace, what you should avoid mixing are C and C++ memory management and I/O - as far as I can remember, that's the only kind of thing that can really give you a headache.