Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Printf/scanf weren broken; why “fix” them with ugly shift operators?

0
Posted

Printf/scanf weren broken; why “fix” them with ugly shift operators?

0

The overloaded shift operator syntax is strange at first sight, but it quickly grows on you. However syntax is just syntax; the real issues are deeper. Printf is arguably not broken, and scanf is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (left/right shift) is, relative to C (printf/scanf): • type safe — type of object being I/O’d is known statically by the compiler rather than via dynamically tested ‘%’ fields • less error prone — redundant info has greater chance to get things wrong C++ I/O has no redundant ‘%’ tokens to get right • faster — printf is basically an “interpreter” of a tiny language whose constructs mainly include ‘%’ fields. The proper low-level routine is chosen at runtime based on these fields. C++ I/O picks these routines statically based on actual types of the args • extensible — perhaps most important of all, the C++ I/O mechanism is Extensible to new user-defined data types (imagine the cha

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.