Addition ASCII in c++ -
i change char variable.
using namespace std; int main() { char q; q='b'; char c=127; //cout << (int)q << endl<< static_cast<int>('z'); char d= static_cast<char>(static_cast<int>((static_cast<int>('z') +19))); cout << (int)d << endl; system("pause"); }
intupt:
-115
why after addition variable assigned -115?
on platform, char
8 bit signed
2's complement type. overflowing type , behaviour of overflowing signed
integral type undefined in c++. don't it.
notes:
8 bit mandated standard.
char
canunsigned
orsigned.
if
unsigned
must have range [0, 255], , 255 + 1 0. overflowunsigned
type is defined.if
signed
can either have range -128 +127 or -127 +127. latter range (1's complement) not allowed c++14. (interestingly that's not breaking change since difference in moving 1's 2's complement observable in malformed program such yours, if @ all!)
a simple remedy? use unsigned char
.
Comments
Post a Comment