Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, I'm quite new to c++. Sorry for asking this seemingly silly questions.

Can std::cout fully replace all the printf could offer?
Especially when printf can easily deal with decimal places in float within the text.
Posted

I would say: define "replacement". Could you? :-)

Think about it: given some desired text output, can you always implement it using std:cout? Of course you can, so, in this aspect, the answer to your question is "yes".

But it will be a "replacement" in terms of convenience, maintainability of code or other aspects? Will it be "better" or "worse" (I wrote quotation marks and I mean it)? Biggest part of it is a matter of personal preferences and personal working styles and habits. Some hate using std:cout, and I kinda understand them, but some may show really good reasons for the near-opposite look at the problem…

—SA
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 15-Nov-15 15:03pm    
5ed; cout only wins because it can accept almost any type as a parameter.

I am not sure what is the behavior of printf in that case.
Sergey Alexandrovich Kryukov 15-Nov-15 17:45pm    
Thank you, Afzaal.
What you mention is not the runtime behavior, it's just a matter of implementing an operator for a type. You cannot deny that one can provide required output from any data...
—SA
The answer is very much simple, in the realm of C++, it already has replaced the printf function. You write the C++ programs like this,

C++
#include <iostream.h>

void main () {
   std::cout << "Hello from cout!";
}


There is no need to include the old C-style, stdio.h and use those functions. So in that case, you should consider using cout.

However, in the realm of C, there is no cout. Thus, in that case it is still printf. As Sergey already said,
Quote:
Some hate using std:cout
Here I am, and I agree that I am one of them. I prefer using printf() because of the format arguments I can provide it with. Like this,

C++
#include <stdio.h>

void main () {
  const char *name = "Afzaal Ahmad Zeeshan";
  int age = 20;
  char *message = "Love for all, hatred for none.";

  printf("My name is %s, \nMy age is %d, \n%s", name, age, message);
}


Now the same in the realm of C++ would be something like this,

C++
#include <iostream>

void main () {
  const char *name = "Afzaal Ahmad Zeeshan";
  int age = 20;
  char *message = "Love for all, hatred for none.";

  std::cout << "My name is " << name << ",\nMy age is " << age << 
               ", \n" << message << std::endl;
}


I don't have to say who won, you know who. :-) But still, stick to cout in C++ programs and do not consider re-using the C functions and libraries. That is exactly why Bjarne created C++. The thing where cout wins is that you can pass almost any type to it in the output stream, and it would be accepted.

References:
Please do read the references and manuals for these two, they may help you in understand a few more things:
1. http://www.cplusplus.com/reference/cstdio/printf/[^]
2. http://www.cplusplus.com/reference/iostream/cout/[^]

Note: In C/C++ an array is indeed a pointer, so I used const char *name instead of char name[]. I hope no offence is raised on that. :-)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Nov-15 17:44pm    
Sure, a 5.
—SA
Afzaal Ahmad Zeeshan 16-Nov-15 2:39am    
Thank you, Sergey. :-)
printf() and scanf() are the C functions which may be replaced by std::cout and std::cin in C++. Thus it can be seen as a question of whether to use <iostream> instead of the traditional <cstdio>. This discusses the 2 approaches and gives some compelling reasons for using <iostream>: https://isocpp.org/wiki/faq/input-output#iostream-vs-stdio[^]

The whole issue is discussed at length here: http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c[^]

Is one approach better than the other? The answer you get will probably be coloured as much by history as by reason. Old C programmers are very comfortable with <cstdio> while newer programmers may prefer to take the canonical C++ approach and use <iostream>.
Are Riff wrote:
Can std::cout fully replace all the printf could offer?
Especially when printf can easily deal with decimal places in float
within the text.

Technically yes because you can inherit from <iostream> and overload operator << so it is entirely up to you: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx[^]
 
Share this answer
 
v7
Comments
Afzaal Ahmad Zeeshan 16-Nov-15 2:43am    
Good resources. 5ed. :-)

I enjoyed the Standard C++ link that you provided.
No.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900