0

I have a question about the number of bytes that a computer normally use to do calculation. First of all, i want you to see the source code below.

source code

printf("%d\n", sizeof(444444444));
printf("%d\n", 444444444);

printf("%d\n", sizeof(4444444444));
printf("%llu\n", 4444444444);

output

4
444444444
8
4444444444

As you can see, the computer never loses the value. If it were to be too big to fit in an int, The computer itself would automatically extend it's type. I think the reason why the computer never loses the value is because it operates originally on big type already at least bigger than the 8-bit container.

Would you guys let me know the overall mechanism? Thank you for your help in advance.

  • When you use a numeric literal, the compiler automatically selects the appropriate data type into which that value will fit. What exactly do you want to know? – Cody Gray - on strike Apr 23 '13 at 04:41
  • @CodyGray You mean to tell me that even if i didn't type the suffix right after a number, The compiler would automatically append appropriate suffix? Sorry for irritating you. But I just want to know exactly what is happening down there. Is it specifically stated in c99? –  Apr 24 '13 at 14:36

1 Answers1

2

This has nothing to do with the "calculation ability of [the] computer".

Your example is all about the size of the integer literal you're dealing with, at the compilation stage. An int on most platforms is four bytes (32 bits). This has a maximum value of 0x7FFF_FFFF or 2147483647. An unsigned int has a maximum of 0xFFFF_FFFF or 4294967295.

The compiler will typically default to int for most integer literals, (as with the 4 byte example). Your next value is 0x1_08e8_d71c which is too big for an int, so the literal is promoted to an 8-byte literal, long long.

This is probably a warning on most compilers.

GCC, (in 32-bit mode, -m32) gives the following warning, because long is only 4 bytes:

warning: integer constant is too large for ‘long’ type

Output

sizeof(int)=4, sizeof(long)=4, sizeof(long long)=8

In 64-bit mode however, (-m64) GCC is cool with it, because long is 8-bytes.

sizeof(int)=4, sizeof(long)=8, sizeof(long long)=8

To remedy this, you should use the LL suffix:

long long val = 4444444444LL;
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thank you for your answer. Is that specified in the c99 standard? –  Apr 23 '13 at 05:02
  • 2
    I'm sure it is, I'm just not sure off the top of my head. I'm sure one of those crazy standards guys will come around and trump me :-) – Jonathon Reinhart Apr 23 '13 at 05:05
  • What is the limit of the promotion? Is that possible for computer to promote a number up to 16byte, 32byte, 64byte? Although i know that's ridiculous because the proper types for 16byte or something like that is not currently found. I just want to know the possibility. –  Apr 23 '13 at 05:17
  • @bis0317 I don't believe any C compilers support native arithmetic for values larger than 8 bytes. If you want to use larger integers, you'll have to use a "big number" library, like [GMP](http://gmplib.org/). – Jonathon Reinhart Apr 23 '13 at 06:33