https://rentry.org/PPP2_p82

// Code derived from Stroustrup's PPP2 book
// § 3.9.2 Unsafe conversions
// -beginning on p 82

#include <iostream>

using std::cout;

int main()
{
  double x = 2.7;

  // lots of code...

  int y = x;  // y becomes 2

  cout << x << '\n'   //
       << y << '\n';

  int  a = 1000;
  char b = a;  // b becomes –24 (on some machines)

  cout << a << '\n'  //
       << b << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p82.cpp && ./a.out

PrevUp

Edit
Pub: 21 Jan 2023 11:27 UTC
Edit: 29 Apr 2023 06:37 UTC
Views: 483