C
ciccio
Why is the multiplication of an int with an unsigned int again unsigned?
#include <iostream>
#include <typeinfo>
int main(void) {
int a = 12;
int b = -12;
unsigned int c = 6;
std::cout << a << " " << typeid(a).name() << std::endl;
std::cout << b << " " << typeid(b).name() << std::endl;
std::cout << a*b << " " << typeid(a*b).name() << std::endl;
std::cout << a*c << " " << typeid(a*c).name() << std::endl;
std::cout << b*c << " " << typeid(b*c).name() << std::endl;
return 0;
}
The output gives
../a.out
12 i
-12 i
-144 i
72 j
4294967224 j
Regards
#include <iostream>
#include <typeinfo>
int main(void) {
int a = 12;
int b = -12;
unsigned int c = 6;
std::cout << a << " " << typeid(a).name() << std::endl;
std::cout << b << " " << typeid(b).name() << std::endl;
std::cout << a*b << " " << typeid(a*b).name() << std::endl;
std::cout << a*c << " " << typeid(a*c).name() << std::endl;
std::cout << b*c << " " << typeid(b*c).name() << std::endl;
return 0;
}
The output gives
../a.out
12 i
-12 i
-144 i
72 j
4294967224 j
Regards