What is the advantage of C++ input and output over C input and output ?
The main benefit of C++ input and output over C input and output is type safety. C++ supports strong type safety, which ensures that the data types used in input and output operations are correctly matched.
In C, the printf and scanf functions rely on format specifiers to interpret the data being printed or read. If the format specifiers do not match the actual data types, it can lead to undefined behavior or runtime errors. For example, using %d to read a floating-point value with scanf can result in unexpected behavior or memory corruption.
On the other hand, C++ introduced the iostream library, which provides cin for input and cout for output. C++ input and output operators ">>" and "<<" are type-safe and perform automatic type conversion when needed. The compiler ensures that the data types of the variables being used in input and output operations are compatible.
By using C++ input and output, you can avoid many type-related errors that can occur in C, leading to more reliable and safer code. Therefore, the correct answer is d) Type safety.