January 2020
Intermediate to advanced
454 pages
11h 25m
English
Example 4 demonstrates the principle of least surprise as follows:
#include <stdio.h>#include <iostream>int main(void){ printf("The answer is: %d\n", 42); std::cout << "The answer is: " << 42 << '\n'; return 0;}
As shown in the preceding example, another great example of the principle of least surprise is the difference between printf() and std::cout. The printf() function requires the addition of format specifiers to output integers to stdout. There are many reasons why printf() is not intuitive: