Why we use printf and in which situations | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why we use printf and in which situations

for c++

22nd Aug 2017, 4:11 PM
Yash🏁🔘
Yash🏁🔘 - avatar
5 Answers
+ 5
printf is a standard C function that writes the results to standard output which is the screen. It somehow similar to cout object in C++. We use it, because sometimes we would like to write C code rather than C++
22nd Aug 2017, 4:18 PM
Babak
Babak - avatar
+ 5
Dear R Ry Thank u so much to explain it in details. So the options are: Dumb and fast vs. Smart and slow
22nd Aug 2017, 5:06 PM
Babak
Babak - avatar
+ 5
very very thanks
22nd Aug 2017, 5:25 PM
Yash🏁🔘
Yash🏁🔘 - avatar
+ 3
we no more need printf in c++ , but yeah we need sprintf , and cprintf In c++ . We use cout instead of printf . Sometimes printf works efficiently like in I the below case :- printf("The value of the result is %c",&a); Something like this .
22nd Aug 2017, 4:20 PM
RZK 022
RZK 022 - avatar
+ 1
Just to add a bit to the other babak's answer, printf also tends to be much faster at outputting than std::cout because it is a simple function call while cout involves overloading operators from derived classes. printf can be easier to use than cout when you have to print something in more complex string formats. For example, to specify the width of what is printed, there is often less typing. int i = 50; printf("%*d", 5, i); using namespace std; cout << setw(5) << i; But cout does type checking while printf doesn't. printf uses an ellipsis and treats additional arguments according to what was put in the format string (first argument). Above, printf expected an integer for the width and the integer to print.
22nd Aug 2017, 4:36 PM
R- Ry
R- Ry - avatar