views:

170

answers:

4

Given the following snippet:

#include <stdio.h>

typedef signed long long int64;
typedef signed int int32;
typedef signed char int8;

int main()
{
    printf("%i\n", sizeof(int8));
    printf("%i\n", sizeof(int32));
    printf("%i\n", sizeof(int64));

    int8 a = 100;
    int8 b = 100;
    int32 c = a * b;
    printf("%i\n", c);

    int32 d = 1000000000;
    int32 e = 1000000000;
    int64 f = d * e;
    printf("%I64d\n", f);
}

The output with MinGW GCC 3.4.5 is (-O0):

1
4
8
10000
-1486618624

The first multiplication is casted to an int32 internally (according to the assembler output). The second multiplication is not casted. I'm not sure if the results differ because the program was running on a IA32, or because it is defined somewhere in the C standard. Nevertheless I'm interested if this exact behavior is defined somewhere (ISO/IEC 9899?), because I like to better understand why and when I've to cast manually (I've problems porting a program from a different architecture).

+4  A: 

The problem is that the multiplication is int32 * int32, which is done as int32, and the result then assigned to an int64. You'd get much the same effect with double d = 3 / 2;, which would divide 3 by 2 using integer division, and assign 1.0 to d.

You have to pay attention to the type of an expression or subexpression whenever it may matter. This requires making sure the appropriate operation is calculated as the appropriate type, such as casting one of the multiplicands to int64, or (in my example) 3.0 / 2 or (float)3 / 2.

David Thornley
Very well put. @azraiyl should change that line to `int64 f = (int64)d * e;`
Paul Tomblin
Sorry that I've not stated that I know the solution here. What I'm interested is why multiplication is int32 * int32 in the first case and not int8 * int8. Even if the CPU supports only int32 multiplication it can be casted back to a int8 after the multiplication. But the IA32 imul instruction works for the 8-Bit registers (AL, ...).
azraiyl
@azrayl: At least in C90, C promoted all arithmetic operands to `int` if they were of smaller types. A glance at the C99 standard suggests that this is no longer the case, but I'm not really sure. What C compiler are you using, and, if applicable, with which options?
David Thornley
+1  A: 

a * b is calculated as an int, and then cast to the receiving variable type (which just happens to be int)

d * e is calculated as an int, and then cast to the receiving variable type (which just happens to be int64)

If either of the type variables were larger that an int (or were floating point), than that type would have been used. But since all the types used in the multiplies were int or smaller, ints were used.

James Curran
+1  A: 

Read K&R (the original). All integer operations are done with the natural integer type unless it involves variables that are (or are casted) to something bigger. The operations on char are casted to 32 bits because that's the natural size of integer on that architecture. The multiplication of the two 32 bit integers is done in 32 bits because nothing is casting it to anything bigger (until you assign it to the 64 bit variable, but that's too late). If you want the operation to happen in 64 bits, cast one or both ints to 64 bits.

int64 f = (int64)d * e;
Paul Tomblin
+6  A: 

The C99 standard does specify that binary operators such as * do not operate on integer types smaller than int. Expressions of these types are promoted to int before the operator is applied. See 6.3.1.4 paragraph 2 and the numerous occurrences of the words "integer promotion". But this is somewhat orthogonal to the assembly instructions generated by the compiler, which operate on ints because this is faster even when the compiler would be allowed to computed a shorter result (because the result is immediately stored in an l-value of a short type, for instance).

Regarding int64 f = d * e; where d and e are of type int, the multiplication is done as an int in accordance with the same promotion rules. The overflow is technically undefined behavior, you are getting two-s-complement result here, but you could get anything according to the standard.

Note: the promotion rules distinguish signed and unsigned types when promoting. The rule is to promote smaller types to int unless int cannot represent all values of the type, in which case unsigned int is used.

Pascal Cuoq
+1 for explaining why the first multiplication works.
Justin Ardini