+ 5

What is the difference between printf() in C and cout in C++ ?

Printf() vs cout Both are used for outputting Is there any other difference between it Other than outputting the data?

8th Sep 2020, 6:07 AM
Yogeshwaran P
Yogeshwaran P - avatar
13 Antworten
+ 8
Unlike printf, cout (and other output streams) are type safe. When you do something like printf("%d",x);, printf will treat x as an integer, no matter what type it actually is. This can be disastrously unsafe if you make a mistake. With cout, the types are all inferred at compile time, and there is no way to make this kind of mistake. On the other hand, complex formatting is much easier to do with printf. You can format cout, but it is much more verbose, with lots of insertion of strange objects with names like precision and hex. Even then it doesn't have quite as many options as printf. Using cout is usually more verbose in general, even without formatting. So even though cout can do much of the formatting that printf can do, printf has hung onto the "best for easy formatting" niche, even in C++ code.
8th Sep 2020, 6:12 AM
Arsenic - avatar
8th Sep 2020, 7:20 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 4
cout is slightly less verbose for unformatted output.
8th Sep 2020, 10:11 PM
Sonic - avatar
+ 3
AFAIK the latter is not buffered but the first is, hence the latter is FASTER THAN THE FIRST ­čŹ╗
9th Sep 2020, 5:10 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
The performance of cin/cout can be slower than scanf/printf because they need to keep themselves aligned with the c library. C is a subset of c++ and in most cases, generally c is faster than c++. In some scenarios, you might face the error of Time Limit Exceeded if you use cin/cout
8th Sep 2020, 9:27 AM
Jenson Y
+ 2
Ok thanks Pallavi Jha ­čśâ
8th Sep 2020, 12:49 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Thank you all­čśâ for clearing my doubt...
8th Sep 2020, 12:34 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Pallavi Jha I amazed that printf() function work in C++­čś« Can I use any C library and it's predefined function in C++ ?­čś░
8th Sep 2020, 12:37 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Wow super­čĹĆ­čĹĆ­čĹŹ Pallavi Jha Can we include other c library in c++?
8th Sep 2020, 12:44 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
printf() in c is a function while cout in c++ is predefined stream object of ostream class...!!
8th Sep 2020, 8:35 PM
Mohammed Sufiyan
Mohammed Sufiyan - avatar
+ 1
you have to use acess specifier in the case of printf according to the variable but in cout there is no need for that
10th Sep 2020, 4:52 AM
swetav savarn
swetav savarn - avatar
+ 1
10th Sep 2020, 4:53 AM
Neel Vysh
Neel Vysh - avatar
- 1
9th Sep 2020, 1:40 PM