1

not a question about a particular code, but in C++ the operators scanf/printf using <stdio.h> seem the same to me as cout or cin in the header <iostream> so can anyone just explain what the difference between the operators, why and when do we use either one? humbly apologize if this has been asked before, but

Thanks.

Jos
  • 65
  • 7
  • While C++ _can talk to C_, C and C++ are two different languages. In C++, the idiomatic way of sending data to standard output is via the `std::cout` object and the `<<` operator. This object is defined inside a standard library header ``. C libraries in Portable C++ are prefixed with `c`. So `` becomes `` and there is no `.h` extension. There is no `` header in C++ standard library. – Ron May 10 '20 at 18:16
  • So it's a library for input, output that was first implemented in C then became portable in C++, correct? – Jos May 10 '20 at 18:18
  • Does this answer your question? [C++ Streams vs. C-style IO?](https://stackoverflow.com/questions/5328873/c-streams-vs-c-style-io) – eesiraed May 10 '20 at 18:25
  • While it does answer what I am looking for, it is very detailed, I am just slightly confused with some of the terms. Some answers say that printf is faster than C++ streams. Faster in terms of what exactly? if it is faster (which probably also means better) why is it unsafe to use in C++? I also don't understand an answer that said something about having divergence. – Jos May 10 '20 at 18:33
  • There I will quote "_you can't have divergence between the type of the object and the type specified in the format string_". – Jos May 10 '20 at 18:34
  • @JohnnyJoestar What that means is that with printf you can ask to print one type but supply a different type. For instance `printf("%s", 123);` `%s` is the notation to print an string, but what's been supplied is an integer. That mistake is likely to crash your program. But the possibility of that kind of mistake doesn't exist in C++ I/O because it uses the C++ type system instead of a format string to decide what to print. – john May 10 '20 at 18:37
  • printf is not unsafe to use in C++, or rather it's equally unsafe in C and C++. – john May 10 '20 at 18:38

3 Answers3

1

scanf and printf are not operators. They are functions.

There is no header <iostream.h> in the C++ standard library in any standard version of C++. You may be using an ancient dialect of C++.

when to use which?

Rough rule of thumb: Use std::cout and std::cin when you write C++. Don't use printf or scanf when writing C++.

then why was it imported to C++

As far as I understand, this is so that C programs could be converted to C++ in small incremental steps. If you have written a C program that uses C standard I/O, then you can rewrite the program in C++ without replacing the I/O part entirely. This design choice was probably to encourage existing programs / projects to move from using C to using C++.

The C I/O functions are a vestigial feature that exist for backwards compatiblity with C.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Sir, I hope my question doesn't sound dumb. What you are saying is when coding C++ don't use `scanf` or `printf` then why was it imported to C++ if we don't want to use it? It definitely has special uses in C++, that is precisely why I asked. – Jos May 10 '20 at 18:35
1

scanf and printf come from C and taken up by C++. std::cout/cin are C++ only.

stackoverblown
  • 734
  • 5
  • 10
1

Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”.

cin/cout is faster than scanf/printf, key differnce among these.

Why is scanf faster than cin?

On a high level both of them are wrappers over theread() system call, just syntactic sugar. The only visible difference is that scanf() has to explicitly declare the input type, whereas cin has the redirection operation overloaded using templates. This does not seem like a good enough reason for a performance hit of 5x.

It turns out that iostream makes use of stdio‘s buffering system. So, cin wastes time synchronizing itself with the underlying C-library’s stdio buffer, so that calls to bothscanf()and cin can be interleaved.

The good thing is that libstdc++ provides an option to turn off synchronization of all the iostream standard streams with their corresponding standard C streams using

std::ios::sync_with_stdio(false);

same with cout and printf.

but in simple words, cin, cout uses extraction and insertion in c++ which are basically overloaded, hence another factor of slow speed.

I hope this answer your question of why one is preferred over another and they are basically the way to input data, and internally cin, cout is written using c stdio buffer library.

Ayush Mishra
  • 267
  • 3
  • 14