diamond symbol with cout
(unsigned char)4.6
is truncated to 4
.
But when you cout
a char
it prints the corresponding character (eg. cout << char(65)
will print A
). If you want to print the value of your char
then you need to cast it first to a wider integer type, eg.:
double y = 4.6;
char x = y; // x == 4
cout << (int)x; // prints "4"
// ^^^^^
I get a huge number like 1717986918
The problem is with your printf
, you're using the wrong format specifier (%d = signed integer
) compared to the argument you provide (a char
). But you already figured that out, I'm just making my answer complete. ;)
Again, you probably want to cast it first:
printf("%d", (int)x);