Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sizeof(long long) return 8? [duplicate]

Tags:

c

sizeof

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;
}
like image 908
dinindu Avatar asked Oct 29 '25 07:10

dinindu


1 Answers

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.

like image 75
Eric Postpischil Avatar answered Oct 31 '25 00:10

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!