2

I start to learn C++ from C. Recently, I have just read a tutorial book about C++. In section Introduce to streams, the book has noted:

The << operator is overloaded so that the operand on the right can be a string or any primitive value. If this operand is not a string, the << operator converts it to string form before sending it to the output stream.

So I wonder whether printf() function in C has the same effect. And if it doesn't, please tell me about the differences between both of them.

bolov
  • 72,283
  • 15
  • 145
  • 224
mja
  • 1,273
  • 1
  • 20
  • 22
  • 2
    possible duplicate http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c – Khalil Khalaf Apr 22 '16 at 13:57
  • 3
    `printf` does that kind of conversion with [format specifiers](http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm). e.g `int test = 1; printf ("%d", test); ` – LPs Apr 22 '16 at 13:58
  • 1
    Read about [`sprintf`](http://www.cplusplus.com/reference/cstdio/sprintf/), this may be interesting for you. – Jabberwocky Apr 22 '16 at 14:04

2 Answers2

7

Well, of course it has to somehow generate a string representation of each argument, that is needed in order to have something to print. Printing involves sending streams of characters to an output device after all, you can't print unless you have a sequence of characters.

The printf() function uses the formatting string to control how to interpret each argument in order to create the character representation, and also how to format that representation when output.

Note that no "conversion" of the arguments happens that is visible externally, of course. There's no way

printf("%d\n", 47);

can make that 47 into a string in place; C uses call by value so the function only gets a copy of the value, and it then uses the type information implicit in the %d conversion specifier to figure out how to generate the two characters '4' and '7' that make up the printed representation.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

So I wonder whether printf() function in C has the same effect.

Both C and C++ uses streams for output. In C it is stdout and in C++ it is cout.

Though it is not evident from the statement printf writes to standard output(stdout) say a terminal.

In case of cout, it is evident from a statement itself, where the output is going.

Some subtle differences

With cout you might need to include an additional header - say iomanip - and use some functions - say setw() - to have fine formatting where as in printf you rely on format string.

Performance - Each has its own advantage depending on what you print and where you print. I borrowed this point from here.

Another similarity

Both C++ and C standards mention nothing about the order of evaluation of function arguments. So you must not try fancy stuff with functions. For example neither should you do

printf(%d%d",++i,i++); // The behaviour is undefined.

nor should you do

cout<<++i<<++i; // The behaviour is undefined.

Note:

Remember that the c streams are available in C++ if you include the necessary headers.

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102