0

I am working on a new API for reading GRIB2 files - part of an open-source library for meteorologists and climate scientists.

The library needs to handle 64-bit integers (in addition to 8, 16, and 32 bit integers). Signed and unsigned integer types must be handled.

In the netcdf-c library, we use unsigned long long:

int
nc_put_att_ulonglong(int ncid, int varid, const char *name, nc_type xtype,
                     size_t len, const unsigned long long *op);

But we also sometimes use size_t:

int
nc_inq_grpname_full(int ncid, size_t *lenp, char *full_name);

Even though I wrote that function, I can't remember why I chose size_t instead of unsigned long long. ;-)

In writing a library for general use, is there a good reason to prefer size_t to unsigned long long?

And now that I am writing a new API, should I use the uint64_t type? It seems most suitable for representing something that is actually 64 bits in the data file.

Edward Hartnett
  • 882
  • 7
  • 16

1 Answers1

0

I prefer size_t to unsigned long long because size_t is more compact and unsigned long long is also a bit confusing. I prefer uint64_t to size_t because uint64_t is more precise.

sizeof(bool) = 1
sizeof(char) = 1
sizeof(double) = 8
sizeof(float) = 4
sizeof(int) = 4
sizeof(long double) = 8
sizeof(long long) = 8
sizeof(long) = 8
sizeof(short) = 2
sizeof(size_t) = 8
Charlie Zender
  • 5,929
  • 14
  • 19