How to specify 64 bit integers in c

后端 未结 5 1932
予麋鹿
予麋鹿 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 00:38

    Use int64_t, that portable C99 code.

    int64_t var = 0x0000444400004444LL;
    

    For printing:

    #define __STDC_FORMAT_MACROS
    #include <inttypes.h>
    
    printf("blabla %" PRIi64 " blabla\n", var);
    
    0 讨论(0)
  • 2020-12-08 00:47

    Try an LL suffix on the number, the compiler may be casting it to an intermediate type as part of the parse. See http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html

    long long int i2 = 0x0000444400004444LL;

    Additionally, the the compiler is discarding the leading zeros, so 0x000044440000 is becoming 0x44440000, which is a perfectly acceptable 32-bit integer (which is why you aren't seeing any warnings prior to f2).

    0 讨论(0)
  • 2020-12-08 00:53

    Append ll suffix to hex digits for 64-bit (long long int), or ull suffix for unsigned 64-bit (unsigned long long)

    0 讨论(0)
  • 2020-12-08 00:56

    Use stdint.h for specific sizes of integer data types, and also use appropriate suffixes for integer literal constants, e.g.:

    #include <stdint.h>
    
    int64_t i2 = 0x0000444400004444LL;
    
    0 讨论(0)
  • 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 <stdint.h>
    #include <inttypes.h>
    
    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
    
    0 讨论(0)
提交回复
热议问题