fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <type_traits>
  4. using namespace std;
  5.  
  6. int main() {
  7. // Incorrect input/output of an int_fast8_t variable
  8. int_fast8_t integer_variable;
  9. cin >> integer_variable; // 84 in the input
  10. cout << static_cast<int>(integer_variable) << endl; // the value of the variable is 56 (ASCII code of the character '8')
  11. cout << integer_variable << endl; // character '8' printed – the character of the ASCII code 56
  12. // The foundation of the above problems: int_fast8_t is, in this implementation, bond to signed char
  13. cout << is_same<int_least8_t, signed char>::value << endl;
  14.  
  15. // Ignoring garbage left in the input, that is the digit of 4
  16. cin.ignore();
  17.  
  18. // However, a variable of the type int_least16_t works correctly
  19. int_least16_t another_integer_variable;
  20. cin >> another_integer_variable; // Number 84 in the input
  21. cout << static_cast<int>(another_integer_variable) << endl; // The value of the variable – 84
  22. cout << another_integer_variable << endl; // The variable is being printed correctly
  23. // This works, since in this implementation int_least16_t is the simple short int
  24. cout << is_same<int_least16_t, short int>::value << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 3472KB
stdin
84

84
stdout
56
8
1
84
84
1