[英]Why “unsigned int64_t” gives an error in C?
為什么以下程序出錯?
#include <stdio.h>
int main()
{
unsigned int64_t i = 12;
printf("%lld\n", i);
return 0;
}
錯誤:
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
但是,如果我刪除unsigned關鍵字,它工作正常。 那么, 為什么unsigned int64_t i
給出錯誤?
您不能在int64_t
類型上應用unsigned
修飾符。 它只適用於char
, short
, int
, long
和long long
。
您可能想要使用uint64_t
,它是int64_t
的無符號對應物。
另請注意int64_t
等。 在標題stdint.h
中定義,如果要使用這些類型,則應包括該標題。
int64_t
不是一些內置類型。 嘗試添加#include <stdint.h>
來定義這些類型; 然后使用uint64_t
,這意味着你想要的東西。 心連心
int64_t
是一個typedef名稱 。 N1570§7.20.1.1p1:
type_f name int N _t指定一個有符號整數類型,其寬度為N ,沒有填充位和二進制補碼表示。 因此, int8_t表示這樣的帶符號整數類型,其寬度恰好為8位。
標准列出了§6.7.2p2中合法的組合:
- 燒焦
- 簽名的char
- 無符號字符
- short,signed short,short int或signed short int
- unsigned short或unsigned short int
- int,signed或signed int
- unsigned或unsigned int
- long,signed long,long int或signed long int
- unsigned long,或unsigned long int
- long long,signed long long,long long int,或signed long long int
- unsigned long long,或unsigned long long int
...
- typedef名稱
與問題無關的類型已從列表中刪除。
請注意如何將typedef名稱與unsigned
混合。
要使用無符號64位類型,您需要:
使用不帶unsigned
說明符的uint64_t
(注意前導u
)。
uint64_t i = 12;
包括定義uint64_t
stdint.h
(或inttypes.h
)。
要打印uint64_t
您需要包含inttypes.h
並使用PRIu64
:
printf("%" PRIu64 "\\n", i);
您也可以轉換為unsigned long long
,即64位或更多。 但是,當它不是絕對必要時,最好避免施放,所以你應該更喜歡PRIu64
方法。
printf("%llu\\n", (unsigned long long)i);
int64_t
typedef
是這樣的:
typedef signed long long int int64_t;
所以, unsigned int64_t i;
變成這樣的東西:
unsigned signed long long int i;
這顯然是一個編譯器錯誤。
因此,使用int64_t
而不是unsigned int64_t
。
另外,在程序中添加#include <stdint.h>
頭文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.