HelpMePlease
HelpMePlease

Reputation: 127

why does gcc have to recognize int64 as long int?

This is how LWT_ELEMID defined in liblwgeom_topo.h:

typedef int64_t LWT_INT64;
typedef LWT_INT64 LWT_ELEMID;

I include this .h file and define some argument in LWT_ELEMID type.

But it always warns me like this: /home/user/xxx.c:880:36: warning: format '%lld' expects argument of type 'long long int', but argument 3 has type 'LWT_ELEMID' {aka 'const long int'} [-Wformat=]

or like this: /home/user/xxx.c:3034:19: note: expected 'LWT_ELEMID *' {aka 'long int *'} but argument is of type 'lint64 *' {aka 'long long int *'}

my environment:

Thread model: posix

gcc version: 8.3.0 (Ubuntu 8.3.0-6ubuntu1)

Target: x86_64-linux-gnu

Upvotes: 0

Views: 187

Answers (2)

umläute
umläute

Reputation: 31364

The reason is the underlying data model of your target platform.

What you are seeing is the LP64 data model (long (as opposed to long long) and pointer are 64bit), which according to Wikipedia is used by

Most Unix and Unix-like systems, e.g., Solaris, Linux, BSD, macOS. Windows when using Cygwin; z/OS

whereas the LLP64 data model (long long (as opposed to ordinary long) and pointer are 64bit) is (according to the same source) prevalent on

Microsoft Windows (x86-64 and IA-64) using Visual C++; and MinGW

since int64_t are (by definition) always 64bit integers, and in the LP64 model long int are also 64bit integers, the two types are the same.

Upvotes: 2

Lundin
Lundin

Reputation: 214780

For strange reasons, gcc for Linux went with making long 64 bits (even though long long has been available forever), thereby breaking backwards compatibility with 32 bit systems. So you have to use %ld, or better yet, avoid this whole long debacle and use the portable PRIi64 conversion specifier from inttypes.h.

Example:

#include <inttypes.h> // includes stdint.h internally
#include <stdio.h>

int main (void)
{
  typedef int64_t LWT_INT64;
  typedef LWT_INT64 LWT_ELEMID;
  LWT_ELEMID x = 123;

  printf("%"PRIi64 "\n", x); // PRIi64 is a string literal, hence this syntax
}

Upvotes: 3

Related Questions