The answer is very much simple, in the realm of C++, it already has replaced the
print
f function. You write the C++ programs like this,
#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,
#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,
#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. :-)