tags:

views:

103

answers:

3

Im wondering about this: when I try to assign an integer value to an int variable (16-bit compiler, 2 bytes for integers) lets say:

int a;

a=40000;

that can't be represented with the range of the type it will be truncated. But what im seeing is that the resulting value in a is the bit pattern for -25000 (or some close number) wich means that the binary representation that the compiler choose for decimal 40000 was unsigned integer representation. And that raises my question: how does the compiler chooses the type for this literal expressions?

Im guessing it uses the type capable of handling the value with less storage space needed.

A: 

40000 in hexadecimal is 0x9C40. Notice that the most significant bit of this value is a 1 (0x8000). Assuming a two's complement representation, that means that if you cram 0x9C40 into a signed representation, then, by subtracting the value at the sign bit from the rest, you will get 0x1C40 - 0x8000 = -0x63C0 = -25536 (base 10).

Have a look at INT00-C. Understand the data model used by your implementation(s) and INT02-C. Understand integer conversion rules on the CERT C Secure Coding Standard Wiki for more information.

Andrew Keeton
True and relevant, but I think the actual question is "how does the compiler choose the type for the literal", not "what happens when I assign to int from that type".
Steve Jessop
+1  A: 

From Kernighan & Ritchie, Appendix A2.5.1 (Integer Constants), p 193:

The type of an integer constant depends on its form, value and suffix...If it is unsuffixed and decimal, it has the first of these types in which its value can be represented: int, long int, unsigned long int.

Note that this answer is only relevant to C89, as the 2nd Edition of the "C Programming Language" predates the C99 standard.

Matt Nizol
+2  A: 

Behaviour here differs between C89 and C99.

In C89, an integer literal takes the first of these types in which it can be represented:

int, long, unsigned int

In C99, an integer literal takes the first of these types in which it can be represented:

int, long, long long

For your particular code snippet it makes no difference, since 40000 is guaranteed to fit in a long, but there are a few significant differences between C89 and C99 literals.

Some of those consequences are described here:

http://www.hardtoc.com/archives/119

Steve Jessop