+ 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?
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.
+ 6
https://www.quora.com/What-is-the-difference-between-printf-cout-in-C++#:~:text=Originally%20Answered%3A%20What%20is%20the%20difference%20between%20cout%20and%20printf%3F,-Both%20cout%20and&text=cout%20is%20a%20output%20stream,both%20c%20and%20c%2B%2B.
+ 4
cout is slightly less verbose for unformatted output.
+ 3
AFAIK the latter is not buffered but the first is, hence the latter is FASTER THAN THE FIRST 🍻
+ 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
+ 2
Ok thanks Pallavi Jha 😃
+ 1
Thank you all😃
for clearing my doubt...
+ 1
Pallavi Jha I amazed that printf() function work in
C++😮
Can I use any C library and it's predefined function in
C++ ?😰
+ 1
Wow super👏👏👍 Pallavi Jha
Can we include other c library in c++?
+ 1
printf() in c is a function while cout in c++ is predefined stream object of ostream class...!!
+ 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
+ 1
- 1
yes