cout vs printf() 
Author Message
 cout vs printf()


Quote:

> I am currently teaching myself c via books and projects spun from thin air.
> I notice that in a lot of sample code provided to other questions the
> construct:

> cout << "yadayada" << flush;

First close the book and look at the cover...

If it does not say C++ rather than C, trash the book as useless
as the author either does not know the difference or is writing
in so muddy a fashion that you don't know it.

The direction of I/O via streams is a C++ concept and not a
C one in general and belongs in a C++ group instead of this one.

If you are really trying to learn C, then get a new book. If you
are trying to learn C++, learn the streams syntax as you will
see a LOT more of that than 'printf' style syntax (and take your
questions one room over at comp.lang.c++).

--
Shawn Lavin
As always, my opinions are my own



Fri, 28 Sep 2001 03:00:00 GMT  
 cout vs printf()
I am currently teaching myself c via books and projects spun from thin air.
I notice that in a lot of sample code provided to other questions the
construct:

cout << "yadayada" << flush;

is used frequently for output to the console (presumed).  My question is why
is this construct used instead of printf()?  What is the difference between
the two constructs and in what situations would a programmer choose one over
the other?

Any information would be appreciated.

Thank you.

James Pluck



Sat, 29 Sep 2001 03:00:00 GMT  
 cout vs printf()

Thank you for your response (and the response of James McIninch via email)

Both of you said approximately the same thing.  I did not realise this was a
c++ construct.  I'll direct my msg over there -->

Regards
James.



Sat, 29 Sep 2001 03:00:00 GMT  
 cout vs printf()

Quote:

> I am currently teaching myself c via books and projects spun from thin air.
> I notice that in a lot of sample code provided to other questions the
> construct:

> cout << "yadayada" << flush;

> is used frequently for output to the console (presumed).  My question is why
> is this construct used instead of printf()?  What is the difference between
> the two constructs and in what situations would a programmer choose one over
> the other?

> Any information would be appreciated.

> Thank you.

> James Pluck

They do the same thing, 'cout' is used in C++ programs, and printf() is
used C programs.

Brad Proctor

--

Home Page: "http://members.xoom.com/fredlie/"



Wed, 03 Oct 2001 03:00:00 GMT  
 cout vs printf()

Quote:


>> I am currently teaching myself c via books and projects spun from thin
air.
>> I notice that in a lot of sample code provided to other questions the
>> construct:

>> cout << "yadayada" << flush;

>> is used frequently for output to the console (presumed).  My question is
why
>> is this construct used instead of printf()?  What is the difference
between
>> the two constructs and in what situations would a programmer choose one
over
>> the other?

>They do the same thing, 'cout' is used in C++ programs, and printf() is
>used C programs.

On the rare occasions when I write C++, I use printf. It's just as valid in
C++ as it is in C.

cout gives me a headache as I try to resolve all those left shift ops :-)



Fri, 05 Oct 2001 03:00:00 GMT  
 cout vs printf()

Quote:


> > I am currently teaching myself c via books and projects spun from
thin air.
> > I notice that in a lot of sample code provided to other questions
the
> > construct:

> > cout << "yadayada" << flush;

> > is used frequently for output to the console (presumed).  My
question is why
> > is this construct used instead of printf()?  What is the
difference between
> > the two constructs and in what situations would a programmer
choose one over
> > the other?

> > Any information would be appreciated.

> > Thank you.

> > James Pluck

> They do the same thing, 'cout' is used in C++ programs, and printf()
is
> used C programs.

Not quite the same thing.  Let's see:

// Original C version
int VarToShow = 1;
...
void Output()
{
   printf( "%d\n", VarToShow );

Quote:
}

// Original C++ version
int VarToShow = 1;
...
void Output()
{
   std::cout << VarToShow << std::endl;

Quote:
}

And later we modify the files...

// New C version
long VarToShow = 1;
...
void Output()
{
   printf( "%d\n", VarToShow );

Quote:
}

// Original C++ version
long VarToShow = 1;
...
void Output()
{
   std::cout << VarToShow << std::endl;

Quote:
}

Oops.  We've introduced a bug into the new C version - we need to
update the printf() format string, but we didn't - perhaps because it
was buried in ump{*filter*} thousand other lines of code and we simply
missed it.  The C++ version, on the other hand, doesn't give a damn -
it simply takes the new long variable and does its thing.


Sat, 06 Oct 2001 03:00:00 GMT  
 cout vs printf()


Quote:
>And later we modify the files...

>// New C version
>long VarToShow = 1;
>...
>void Output()
>{
>   printf( "%d\n", VarToShow );
>}

>// Original C++ version
>long VarToShow = 1;
>...
>void Output()
>{
>   std::cout << VarToShow << std::endl;
>}

>Oops.  We've introduced a bug into the new C version - we need to
>update the printf() format string, but we didn't - perhaps because it
>was buried in ump{*filter*} thousand other lines of code and we simply
>missed it.  The C++ version, on the other hand, doesn't give a damn -
>it simply takes the new long variable and does its thing.

I compiled the former under GCC and got this:

        test.c: In function `Output':
        test.c:7: warning: int format, long int arg (arg 2)

Use a good compiler. :)

The C++ iostream model is an overblow sham. You picked some easy examples.
It's not so easy to use cout when you have to do precise formatting, in which
you set up base, field width and precision. The manipulators to set up these
properties are a pain to deal with compared to the condensed notation of the
printf conversion specifiers.

Compare
        printf("answer = %8.3f\n", 3.14);

with
        cout "answer = " << width(8) << precision(3) << answer << endl;

What a verbose pain, especially when you have a lot of conversions
to do. You might as well program in COBOL.

I stick to FILE * streams when working in C++.



Sun, 07 Oct 2001 03:00:00 GMT  
 cout vs printf()


Quote:
> I stick to FILE * streams when working in C++.

Glad to hear I'm not the only one. :-)


Sun, 07 Oct 2001 03:00:00 GMT  
 cout vs printf()

Quote:



> > I stick to FILE * streams when working in C++.

> Glad to hear I'm not the only one. :-)

Not by a long shot.  For some obscure reason, C++ fanatics point to the
god-awful and unreadable iostreams junk as a reason to switch to C++.  There are
reasons to use C++, but the mistakes and {*filter*}s that litter it do not count
as evidence for me.

--




Sun, 07 Oct 2001 03:00:00 GMT  
 cout vs printf()

Quote:




>>> I stick to FILE * streams when working in C++.

>> Glad to hear I'm not the only one. :-)

> Not by a long shot.  For some obscure reason, C++ fanatics point to the
> god-awful and unreadable iostreams junk as a reason to switch to C++.

The reason, of course, is that

        cout << ???

is type-safe and extensible in a way that

        printf("%?", ???)

can never be, and this is a real virtue.  (It's not an abstract
philosophical issue, either; much of the hollering about long
long and the C9X standard ends up being closely related.)
But I agree that the syntax leaves something to be desired.

                                        Steve Summit



Sun, 07 Oct 2001 03:00:00 GMT  
 cout vs printf()

Quote:





>>>> I stick to FILE * streams when working in C++.

>>> Glad to hear I'm not the only one. :-)

>> Not by a long shot.  For some obscure reason, C++ fanatics point to the
>> god-awful and unreadable iostreams junk as a reason to switch to C++.

>The reason, of course, is that

>    cout << ???

>is type-safe and extensible in a way that

>    printf("%?", ???)

You could also make the case for cout having a potential performance advantage
due to not having to interpret a formatting string at run time.  That is, if
you say  ``cout << integer'', compile time overload resolution will translate
that to a call to the appropriate function for handling the formatting of the
integral type. However, I'm not too worried about printf being inefficient. :)


Sun, 07 Oct 2001 03:00:00 GMT  
 cout vs printf()
Quote:

>You could also make the case for cout having a potential performance
advantage
>due to not having to interpret a formatting string at run time.  That is,
if
>you say  ``cout << integer'', compile time overload resolution will
translate
>that to a call to the appropriate function for handling the formatting of
the
>integral type. However, I'm not too worried about printf being inefficient.

:)

I would imagine that any potential performance increase from iostreams
would be more than offset by the disgusting bloat of both the language
and its libraries.

   -dave

--
------------------------------------------------------------------------
* Remember to remove the spam blocker when replying.
* Return address is: dstarr AT xnet DOT com
------------------------------------------------------------------------



Sun, 07 Oct 2001 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. cout vs printf

2. printf vs. cout

3. printf vs cout

4. (void)printf() vs. printf()

5. Re(2): Re(2): (void)printf() vs. printf()

6. Re(2): (void)printf() vs. printf()

7. cout compared with printf

8. cout and printf

9. how to do printf or cout

10. printf cout captures and cin interaction

11. stupid question about cout in VS .NET 2002

12. confusion with std::cout and ::cout

 

 
Powered by phpBB® Forum Software