2

why can't an int value in c++ have 8-byte size? I know long is 8 bytes in Linux and Mac. also is there any compiler that has a size of int as 8?

I tried searching for GCC compiler that has a size of int as 8 bytes but could not find anything related to it.

Saveen
  • 4,120
  • 14
  • 38
  • 41
jas
  • 31
  • 4
  • 1
    I think your question could be improved with a) proper punctuation and grammar and b) a [mcve] demonstrating `sizeof(int)` showing you `8` on a Mac and Linux and not 8 on some other compiler. – Tas Aug 07 '18 at 00:48
  • 6
    Possible duplicate of [Does the size of an int depend on the compiler and/or processor?](https://stackoverflow.com/questions/2331751/does-the-size-of-an-int-depend-on-the-compiler-and-or-processor) – WestaAlger Aug 07 '18 at 00:55
  • `#include ` and use `uint64_t` for an 8-byte int. – selbie Aug 07 '18 at 03:56

1 Answers1

3

There is nothing stopping compilers from using an 8-byte int other than the fact that it would probably break tons of existing code.

For the last 20 or so years, people have assumed that an int is 4 bytes long (on x86 and amd64 CPUs, at least). While changing the size of an int would be totally legal under the C++ standard, it would also break tons of old code that relies on the size of an int for things like bit-twiddling. Breaking people's code isn't a good way to convince them they should keep using your compiler, and so compiler makers tend to make compilers that behave like people expect them to, even if that behavior isn't mandated by the language standard.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • So is it safe to assume the range of int will always be -2,147,483,648 to 2,147,483,647 ? – jas Aug 07 '18 at 01:23
  • 1
    @jas No. My point is that the standard does not specify a size for `int`, but that compilers do what they can to avoid breaking legacy code. For new code use the fixed-width types (`int32_t`, `uint64_t`, etc) if you need a specific size. – Miles Budnek Aug 07 '18 at 01:37
  • I see. I understand now. Thank you for your response. – jas Aug 07 '18 at 01:38