3

I'm new to C. sizeof(long) returns 8 because the size of long is 8 bytes (4 bytes for a 32-bit OS).

But I wonder why sizeof(long long) is also 8. Shouldn't it be 16?

#include <stdio.h>
#include <stdint.h>

int main() {
  printf("%zu\n", sizeof(unsigned long));
  printf("%zu\n", sizeof(unsigned long long));

  return 0;
}
Boann
  • 48,794
  • 16
  • 117
  • 146
0xdw
  • 3,755
  • 2
  • 25
  • 40
  • Maybe [this article about C data types](https://en.wikipedia.org/wiki/C_data_types) will give you some light? Pay attention to "..containing at least the...". It is compiler dependent. – fpiette Jun 20 '21 at 17:27
  • 2
    Because on your platform, `long` is 8 and `long long` is 8. On my platform, `int` is 8, `long` is 16, and `long long` is 32. On someone elses platform, `int` might be 2, and `long` might be 4, and `long long` 8. It varies by platform. If you need a specific size, consider `std::int32_t` and `std::int64_t`. – Eljay Jun 20 '21 at 17:31
  • 5
    @Eljay: You have a system where `long` is 16 and `long long` is `32`?!? What platform is that? I've basically never seen a system where `long long` is anything but `8`, `long` is the only one that varies much (being `4` on some systems, `8` on others). (Minor side-note: The question is about C, so no `std::` prefix) – ShadowRanger Jun 20 '21 at 17:34
  • IIRC, in the modern days, `long` is 4 bytes on Windows and 8 elsewhere. In case it's unclear, Eljay isn't speaking about real platforms, but just making a point. – HolyBlackCat Jun 20 '21 at 17:35
  • In any event, Wikipedia has [an article on the various standards for data type sizes on 64 bit machines](https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models), if all you're looking for is "what will be what size, where". – ShadowRanger Jun 20 '21 at 17:36
  • 1
    @HolyBlackCat: I have to assume so, but they didn't phrase it that way, so I was curious if there was a real system like that (I've worked on some weird systems, where literally every type was eight bytes, including `char`, so I wasn't going to rule it out). – ShadowRanger Jun 20 '21 at 17:37
  • @ShadowRanger • it's not a real-real system yet, it's my work's in-house "garage" experimental system. May or may not go anywhere. (The question was previously tagged with `c++`.) – Eljay Jun 20 '21 at 17:40
  • If these sizes were fixed, you wouldn't need the `sizeof` operator. – wildplasser Jun 20 '21 at 17:58
  • Thanks for all answers, I understand now :) – 0xdw Jun 21 '21 at 01:44

1 Answers1

5

The C standard does not require long long to be 16 bytes or to be wider than long.

In C 2018 5.2.4.2.1 1, the standard requires long long to be able to represent numbers from −(263−1) to +(263−1). That requires at least 64 bits, so a long long must be at least 8 8-bit bytes (or, for example, 4 16-bit bytes). It is optional for a C implementation to make long long wider and support a larger range.

In 6.3.1.1 1, the standard requires the rank of long long (also known as long long int) to be greater than the rank of long. Due to the definition of “rank,” this means long long must have at least as much precision as long. (The precision of an integer type is the number of bits used to represent the value, not including the sign bit.) The standard does not require long long to have more precision than long; it can be equal.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312