cout vs printf 
Author Message
 cout vs printf

I am a beginner to c and I have been hearing so much about printf.  I have
been using cout for all my printing on screen.

Could someone please tell me the advantage of using . . .

c = 4;
printf("%d",c);

over . . .

c=4;
cout << 4;

?



Tue, 29 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:

>I am a beginner to c and I have been hearing so much about printf.  I have
>been using cout for all my printing on screen.

>Could someone please tell me the advantage of using . . .

>c = 4;
>printf("%d",c);

>over . . .

>c=4;
>cout << 4;

>?

This really belongs in comp.lang.c++. However, the main advantage is
type checking.

int i = 4;
cout << i;

really results in a function call, that looks like

cout.operator<<(i);

This member function has been overloaded for a variety of types. With
printf, it is up to you to use the proper conversion specifiers and use
an agreeing number of arguments.

You can also overload cout for new types. If you do, this then

object ob;
cout << ob;

will work fine. You cannot write:

printf("%ob",  ob);

This last example may seem a bit gratuitous, but most simple examples
like this do not show any compeling reason to use C++ over C. (Another
advantage is that you can set the state of the stream to format output,

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

because cout is not defined in C (as anything special).
you're looking at a c++ dohickey.

Eric Fulton

/* std.disclaimer applies */

: >I am a beginner to c and I have been hearing so much about printf.  I have
: >been using cout for all my printing on screen.
: >
: >Could someone please tell me the advantage of using . . .
: >
: >c = 4;
: >printf("%d",c);
: >
: >over . . .
: >
: >c=4;
: >cout << 4;
: >
: >?

: This really belongs in comp.lang.c++. However, the main advantage is
: type checking.

: int i = 4;
: cout << i;

: really results in a function call, that looks like

: cout.operator<<(i);

: This member function has been overloaded for a variety of types. With
: printf, it is up to you to use the proper conversion specifiers and use
: an agreeing number of arguments.

: You can also overload cout for new types. If you do, this then

: object ob;
: cout << ob;

: will work fine. You cannot write:

: printf("%ob",  ob);

: This last example may seem a bit gratuitous, but most simple examples
: like this do not show any compeling reason to use C++ over C. (Another
: advantage is that you can set the state of the stream to format output,

: --
: Craig

: Manchester, NH
: The less a man makes declarative statements, the
: less he's apt to look foolish in retrospect.
:   -- Quentin Tarantino in "Four Rooms"



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

: I am a beginner to c and I have been hearing so much about printf.  I have
: been using cout for all my printing on screen.

: Could someone please tell me the advantage of using . . .

: c = 4;
: printf("%d",c);

: over . . .

: c=4;
: cout << 4;

: ?

First and foremost, cout is not a C function. It is a C++ function. And
I think it's actually more of a variable with an overloaded operator
than an actual function.

Printf is more flexible. I find it much easier to make special formats
(like print such-and-such in hex with 4 leading 0's and no more than 4
characters). It's also got companion functions (sprintf and fprintf) which
are easier to understand if you know printf.

Cout, on the other hand, is safer because you don't run the risk of trying
to print a short int with an unsigned long and trashing your stack.
C++ does more type checking and passes the rvalue type to the << operator.

Roger

My opinions, not TI's.

--
Roger Daniel Pease -- Texas Instruments Semiconductor Group



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf


Quote:
>Could someone please tell me the advantage of using . . .

>c = 4;
>printf("%d",c);

>over . . .

>c=4;
>cout << 4;

>?

The first group written with printf is straight C.

The second only works in C++.

Further, you can not portably mix printf's and cout's in one application.

--

Mathematics and Computer Science Major
University of Texas at Tyler



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:

> I am a beginner to c and I have been hearing so much about printf.  I have
> been using cout for all my printing on screen.

> Could someone please tell me the advantage of using . . .

> c = 4;
> printf("%d",c);

> over . . .

> c=4;
> cout << 4;

cout is C++, you don't have cout in normal C.

--

<\___/>
/ O O \
\_____/  FTB.



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf


Quote:

>I am a beginner to c and I have been hearing so much about printf.  I have
>been using cout for all my printing on screen.

>Could someone please tell me the advantage of using . . .

>c = 4;
>printf("%d",c);

>over . . .

>c=4;
>cout << 4;

>?

Errrr... cout << 4 takes the variable cout and shifts the bits
4 steps to the left, whereas printf prints something to stdout.

This is one advantage of printf. (ie. it is c. cout is c++)



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf


Quote:

[...]
>>Could someone please tell me the advantage of using . . .

>>c = 4;
>>printf("%d",c);

>>over . . .

>>c=4;
>>cout << 4;
[...]
>This really belongs in comp.lang.c++.

[...]

Yes, it does belong there.

You got it backward.  He asked the advantage of printf over cout.
You answered the advantage of cout over printf.

If your compiler only accepts printf, you don't have a choice.
A C compiler does not understand cout, only printf.
A C++ compiler understands both.

If you are a C programmer with a C++ compiler, you may very
well use printf from habit, instead of cout.
--



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:


>>Could someone please tell me the advantage of using . . .

>>c = 4;
>>printf("%d",c);

>>over . . .

>>c=4;
>>cout << 4;

>>?

>The first group written with printf is straight C.

>The second only works in C++.

>Further, you can not portably mix printf's and cout's in one application.

I wasn't aware of that. In "The C++ Programming Language" 10.6
Stroustrup says "Typically, C and C++ input and output can be mixed
on a per-line basis. Mixing at a per-character basis is possible on
some implementations but cannot be relied on for portable code. Some
implementations of the C++ stream library require a call of the static
member function ios::sync_with_stdio() when mixing with C I/O."

It would seem that the first example would be portable, and the second
would not.

#include <iostreams.h>
#include <stdio.h>
int main (void)
    {
    printf("Hello!\n");
    cout << "I must be going...\n\n";
    /* Need to call ios::synch_with_stdio() */
    printf("Hel");
    cout << "lo!\n";
    return 0;
    }

Of coarse, this may have changed. :-)

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf


Quote:



>[...]
>>>Could someone please tell me the advantage of using . . .

>>>c = 4;
>>>printf("%d",c);

>>>over . . .

>>>c=4;
>>>cout << 4;
>[...]
>>This really belongs in comp.lang.c++.
>[...]

>Yes, it does belong there.

>You got it backward.  He asked the advantage of printf over cout.
>You answered the advantage of cout over printf.

That's true. I may have been thrown by the fact it was cout vs
printf in the subject line. Also, the fact that there is not one
single advantage to using printf over cout in a C++ program
prevented me from presenting an argument for the use of printf.

Quote:
>If your compiler only accepts printf, you don't have a choice.

Read: "Your stuck with printf".

Quote:
>A C compiler does not understand cout, only printf.
>A C++ compiler understands both.

That is handy.

Quote:
>If you are a C programmer with a C++ compiler, you may very
>well use printf from habit, instead of cout.

Bad habits need to be broken. Printf is a "fools function": no
type checking can be performed without the aid of a lint like
program, it has a crazy promotions scheme, a cryptic little
language must be learned which is surpassed only by COBOL's pic
clause in its absurdness, and even its named is silly. It clearly
should have been called "print".

The only sensible way to deal with this function is to include

#define printf

in all of your C++ programs.

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

: I am a beginner to c and I have been hearing so much about printf.  I have
: been using cout for all my printing on screen.
:
: Could someone please tell me the advantage of using . . .
:
: c = 4;
: printf("%d",c);
:
: over . . .
:
: c=4;
: cout << 4;

1) printf is faster
2) printf will generate much smaller code.

There are essentially three layers for IO.

read/write  <--  scanf/printf  <--  cin/cout

Each layer sits on top of the previous one.  So you can see from that that
printf/scanf would be faster, but read/write are even faster (due in part
that it is not buffered i/o ) :)

--
 _/_/_/_/  _/        _/_/_/_/   T Jones

  _/     _/        _/  _/       NCSU - Department of Computer Science
 _/     _/_/_/_/  _/_/_/        http://krusty.rh.ncsu.edu/~tavaris



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

printf is C style and cout is C++ style



Quote:



> >[...]
> >>>Could someone please tell me the advantage of using . . .

> >>>c = 4;
> >>>printf("%d",c);

> >>>over . . .

> >>>c=4;
> >>>cout << 4;
> >[...]
> >>This really belongs in comp.lang.c++.
> >[...]

> >Yes, it does belong there.

> >You got it backward.  He asked the advantage of printf over cout.
> >You answered the advantage of cout over printf.

> That's true. I may have been thrown by the fact it was cout vs
> printf in the subject line. Also, the fact that there is not one
> single advantage to using printf over cout in a C++ program
> prevented me from presenting an argument for the use of printf.

> >If your compiler only accepts printf, you don't have a choice.

> Read: "Your stuck with printf".

> >A C compiler does not understand cout, only printf.
> >A C++ compiler understands both.

> That is handy.

> >If you are a C programmer with a C++ compiler, you may very
> >well use printf from habit, instead of cout.

> Bad habits need to be broken. Printf is a "fools function": no
> type checking can be performed without the aid of a lint like
> program, it has a crazy promotions scheme, a cryptic little
> language must be learned which is surpassed only by COBOL's pic
> clause in its absurdness, and even its named is silly. It clearly
> should have been called "print".

> The only sensible way to deal with this function is to include

> #define printf

> in all of your C++ programs.

> --
> Craig

> Manchester, NH
> The less a man makes declarative statements, the
> less he's apt to look foolish in retrospect.
>   -- Quentin Tarantino in "Four Rooms"



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:

> I am a beginner to c and I have been hearing so much about printf.  I have
> been using cout for all my printing on screen.

> Could someone please tell me the advantage of using . . .

> c = 4;
> printf("%d",c);

> over . . .

> c=4;
> cout << 4;

Contrary to a couple of postings, cout is neither a variable (Mr.
Johansson) or
a function (Mr. Pease); rather it is an instance of of a class. Further
the
<< is not the left shift operator from standard 'C'; rather it is the
extraction operator.
As several people have pointed out cout (i.e. the iostreams stuff) does
not
exist in Standard C.  So if all you have is a 'C' compiler you are stuck
with printf.  If you have a C++ compiler (as just about anybody on a PC
would) you have the option of using <stdio.h> and printf/scanf or
<iostream.h> and cout/cin.
IMHO... If you have a choice, use iostream.
If has better type checking (every crash a program by doing something
like:
char c;
scanf("%ld", &c); /* Yes I know that is wrong... problem is the
                        compiler *can't* detect the error so it
                        becomes a runtime problem */ )

It is very flexible - it allows you to write custom formatting modifiers
It works for more than primitive types (you can overload the insertion
and
extraction operators so (assume you have a structure S) that the
following
works

        cin >> S; /* Reads entire structure from stdin */
        cout << S;        /* Outputs entire structure to stdout */

Additionally the class creator decides what it means to input the
class/structure.  Extensive error checking can be done to transparently
validate the data.  On output, the << operator might do different things
depending on the value of a data member 'debug' for instance.

Anyway, whole books have been written on this robust functionality.
As has also been pointed out, detailed questions should be
directed to comp.lang.c++

Quote:

> ?



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:

> I would also think that using the
>    #include <stdio>
> printf("hello world"\n);
> saves more space than using
>     #include <iostream.h>
>     cout << "hello world\n";
> a.  stdio.h ==> 7k   compared to   iostream.h ==> 28k
> less space more speed.
> Unless you need other iostream.h functions

I assume you are comparing the size of the headers - this has no bearing
on the size of the compiled code, or the execution speed.  

The headers are just text files containing the function prototypes and
the associated #defines and typedefs - they do not contain the actual
functions, and should not generate any code or reserve any space for
data.

(for the pedantic - standard headers need not actually exist as files -
they may be some form of compiler majik instead...)

--
Peter Bennett, VE7CEI    Vancouver, B.C., Canada

GPS and NMEA info and programs:
ftp://sundae.triumf.ca/pub/peter/index.html
or:
ftp://ftp-i2.informatik.rwth-aachen.de/pub/arndt/GPS/peter/index.html
or: http://vancouver-webpages.com/peter/index.html



Wed, 30 Jun 1999 03:00:00 GMT  
 cout vs printf

Quote:


>: I am a beginner to c and I have been hearing so much about printf.  I have
>: been using cout for all my printing on screen.
>:
>: Could someone please tell me the advantage of using . . .
>:
>: c = 4;
>: printf("%d",c);
>:
>: over . . .
>:
>: c=4;
>: cout << 4;

>1) printf is faster
>2) printf will generate much smaller code.

How much this is so will depend on the implementation. With Borland
C++ cout is about 15% slower than printf. There appears to be about
an 8% overhead to convert cout << to the call to ostream::operator<<
function call (which is inline). In addition, the video I/O seems to
be the biggest time hog, for the longer I run the test, the closer
printf and cout become.

Also, the .exe for the cout version is about 40% larger (but just
for a bear bones program; presumably for a real program this would
not be as large a percentage.

As an aside, when I was constructing these test programs I changed
from an int to a long at one point. Yet, I forgot to change %d to
%ld ($#%&! I hate it when I do that!). The cout version worked fine.

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Thu, 01 Jul 1999 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 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