简体   繁体   English

为什么“unsigned int64_t”在C中出错?

[英]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. 但是,如果我删除unsigned关键字,它工作正常。 So, Why unsigned int64_t i gives an error? 那么, 为什么unsigned int64_t i给出错误?

You cannot apply the unsigned modifier on the type int64_t . 您不能在int64_t类型上应用unsigned修饰符。 It only works on char , short , int , long , and long long . 它只适用于charshortintlonglong long

You probably want to use uint64_t which is the unsigned counterpart of int64_t . 您可能想要使用uint64_t ,它是int64_t的无符号对应物。

Also note that int64_t et al. 另请注意int64_t等。 are defined in the header stdint.h , which you should include if you want to use these types. 在标题stdint.h中定义,如果要使用这些类型,则应包括该标题。

int64_t is not some builtin type. int64_t不是一些内置类型。 Try adding #include <stdint.h> to define such types; 尝试添加#include <stdint.h>来定义这些类型; then use uint64_t which means what you appear to intend. 然后使用uint64_t ,这意味着你想要的东西。 Hth 心连心

int64_t is a typedef name . int64_t是一个typedef名称 N1570 §7.20.1.1 p1: N1570§7.20.1.1p1:

The typedef name int N _t designates a signed integer type with width N , no padding bits, and a two's complement representation. type_f name int N _t指定一个有符号整数类型,其宽度为N ,没有填充位和二进制补码表示。 Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits. 因此, int8_t表示这样的带符号整数类型,其宽度恰好为8位。

Standard lists what combinations are legal in §6.7.2 p2: 标准列出了§6.7.2p2中合法的组合:

  • char 烧焦
  • signed char 签名的char
  • unsigned char 无符号字符
  • short, signed short, short int, or signed short int short,signed short,short int或signed short int
  • unsigned short, or unsigned short int unsigned short或unsigned short int
  • int, signed, or signed int int,signed或signed int
  • unsigned, or unsigned int unsigned或unsigned int
  • long, signed long, long int, or signed long int long,signed long,long int或signed long int
  • unsigned long, or unsigned long int unsigned long,或unsigned long int
  • long long, signed long long, long long int, or signed long long int long long,signed long long,long long int,或signed long long int
  • unsigned long long, or unsigned long long int unsigned long long,或unsigned long long int

... ...

  • typedef name typedef名称

Types that are not relevant to the question have been removed from the list. 与问题无关的类型已从列表中删除。

Note how you cannot mix typedef name with unsigned . 请注意如何将typedef名称与unsigned混合。


To use unsigned 64-bit type, you need to: 要使用无符号64位类型,您需要:

  • Use uint64_t (note the leading u ) without unsigned specifier. 使用不带unsigned说明符的uint64_t (注意前导u )。

     uint64_t i = 12; 
  • Include stdint.h (or inttypes.h ) where uint64_t is defined. 包括定义uint64_t stdint.h (或inttypes.h )。

  • To print uint64_t you need to include inttypes.h and use PRIu64 : 要打印uint64_t您需要包含inttypes.h并使用PRIu64

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

    You can also or cast to unsigned long long which is 64-bits or more. 您也可以转换为unsigned long long ,即64位或更多。 However it's preferable to avoid casting when it's not strictly necessary, so the you should prefer PRIu64 method. 但是,当它不是绝对必要时,最好避免施放,所以你应该更喜欢PRIu64方法。

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

typedef of int64_t is something like: int64_t typedef是这样的:

typedef signed long long int int64_t;

So, unsigned int64_t i; 所以, 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 . 因此,使用int64_t而不是unsigned int64_t

Also, add #include <stdint.h> header file in your program. 另外,在程序中添加#include <stdint.h>头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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