What is the type of the result of a multiplication of two chars in C/C++?
unsigned char a = 70;
unsigned char b = 58;
cout << a*b << endl; // prints 4060, means no overflow
cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow
I expect the result of multiplying two number of type T (e.g., char, short, int) becomes T. It seems it is int
for char
because sizeof(a*b)
is 4.
I wrote a simple function to check the size of the result of the multiplication:
template<class T>
void print_sizeof_mult(){
T a;
T b;
cout << sizeof(a*b) << endl;
}
print_sizeof_mult<char>()
, print_sizeof_mult<short>()
, and print_sizeof_mult<int>()
are 4 and print_sizeof_mult<long>()
is 8.
Are these result only for my particular compiler and machine architecture? Or is it documented somewhere that what type is the output of basic operations in C/C++?