How to specify 64 bit integers in c

后端 未结 5 1933
予麋鹿
予麋鹿 2020-12-07 23:58

I\'m trying to use 64 bit integers in C, but am getting mixed signals as to whether it should be possible.

When I execute the printf:

printf(\"Size o         


        
5条回答
  •  無奈伤痛
    2020-12-08 01:00

    How to specify 64 bit integers in c

    Going against the usual good idea to appending LL.

    Appending LL to a integer constant will insure the type is at least as wide as long long. If the integer constant is octal or hex, the constant will become unsigned long long if needed.

    If ones does not care to specify too wide a type, then LL is OK. else, read on.

    long long may be wider than 64-bit.

    Today, it is rare that long long is not 64-bit, yet C specifies long long to be at least 64-bit. So by using LL, in the future, code may be specifying, say, a 128-bit number.

    C has Macros for integer constants which in the below case will be type int_least64_t

    #include 
    #include 
    
    int main(void) {
      int64_t big = INT64_C(9223372036854775807);
      printf("%" PRId64 "\n", big);
      uint64_t jenny = INT64_C(0x08675309) << 32;  // shift was done on at least 64-bit type 
      printf("0x%" PRIX64 "\n", jenny);
    }
    

    output

    9223372036854775807
    0x867530900000000
    

提交回复
热议问题