简体   繁体   中英

Why “unsigned int64_t” gives an error in C?

Why the following program gives an error?

#include <stdio.h>

int main() 
{
    unsigned int64_t i = 12;
    printf("%lld\n", i);
    return 0;
}

Error:

 In function 'main':
5:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'i'
  unsigned int64_t i = 12;
                   ^
5:19: error: 'i' undeclared (first use in this function)
5:19: note: each undeclared identifier is reported only once for each function it appears in

But, If I remove the unsigned keyword, it's working fine. So, Why unsigned int64_t i gives an error?

You cannot apply the unsigned modifier on the type int64_t . It only works on char , short , int , long , and long long .

You probably want to use uint64_t which is the unsigned counterpart of int64_t .

Also note that int64_t et al. are defined in the header stdint.h , which you should include if you want to use these types.

int64_t is not some builtin type. Try adding #include <stdint.h> to define such types; then use uint64_t which means what you appear to intend. Hth

int64_t is a typedef name . N1570 §7.20.1.1 p1:

The typedef name int N _t designates a signed integer type with width N , no padding bits, and a two's complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

Standard lists what combinations are legal in §6.7.2 p2:

  • char
  • signed char
  • unsigned char
  • short, signed short, short int, or signed short int
  • unsigned short, or unsigned short int
  • int, signed, or signed int
  • unsigned, or unsigned int
  • long, signed long, long int, or signed long int
  • unsigned long, or unsigned long int
  • long long, signed long long, long long int, or signed long long int
  • unsigned long long, or unsigned long long int

...

  • typedef name

Types that are not relevant to the question have been removed from the list.

Note how you cannot mix typedef name with unsigned .


To use unsigned 64-bit type, you need to:

  • Use uint64_t (note the leading u ) without unsigned specifier.

     uint64_t i = 12; 
  • Include stdint.h (or inttypes.h ) where uint64_t is defined.

  • To print uint64_t you need to include inttypes.h and use PRIu64 :

     printf("%" PRIu64 "\\n", i); 

    You can also or cast to unsigned long long which is 64-bits or more. However it's preferable to avoid casting when it's not strictly necessary, so the you should prefer PRIu64 method.

     printf("%llu\\n", (unsigned long long)i); 

typedef of int64_t is something like:

typedef signed long long int int64_t;

So, unsigned int64_t i; becomes something like:

unsigned signed long long int i;  

Which is obviously a compiler error.

So, use int64_t instead of unsigned int64_t .

Also, add #include <stdint.h> header file in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM