无符号整型unsigned intunsigned longusigned long longsize_t比较和格式控制
Posted hereis00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无符号整型unsigned intunsigned longusigned long longsize_t比较和格式控制相关的知识,希望对你有一定的参考价值。
位数比较
由于数据的长度和平台相关,所以基于 64 位系统比较。
Windows | Linux | |
---|---|---|
unsigned int | 32 bits/4294967295 | 32 bits |
unsigned long | 32 bits | 64 bits/18446744073709551615 |
unsigned long long | 64 bits | 64 bits |
size_t | 32 bits | 64 bits |
可以看到,size_t 和 unsigned long 比较同步。另外 ssize_t 和 size_t 的关系就像 int 和 unsigned int 的关系。(参考1,参考2)
格式控制(printf)
length | d i | u o x X | f F e E g G a A | c | s | p | n |
---|---|---|---|---|---|---|---|
(none) | int | unsigned int | double | int | char* | void* | int* |
hh | signed char | unsigned char | signed char* | ||||
h | short int | unsigned short int | short int* | ||||
l | long int | unsigned long int | wint_t | wchar_t* | long int* | ||
ll | long long int | unsigned long long int | long long int* | ||||
j | intmax_t | uintmax_t | intmax_t* | ||||
z | size_t | size_t | size_t* | ||||
t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
L | long double |
(参考)
测试代码
#include <cstdio>
int main()
{
unsigned int ui = -1;
unsigned long ul = -1;
unsigned long long ull = -1;
size_t st = -1;
printf("ui=%u,ul=%lu,ull=%llu,st=%zu
", ui, ul, ull, st);
return 0;
}
测试结果
Windows
Linux
以上是关于无符号整型unsigned intunsigned longusigned long longsize_t比较和格式控制的主要内容,如果未能解决你的问题,请参考以下文章