我们知道,int的大小在大多数计算机里面存储长度是4个字节
如果,最高位被符号位占领,那么他的取值区间就是
- ~ -1
我们可以用一小段代码来计算这个长度
#include <stdio.h>
#include <stdint.h>
#include <wchar.h>
int main(void)
{
printf("PTRDIFF_MIN = %td\n", PTRDIFF_MIN);
printf("PTRDIFF_MAX = %+td\n", PTRDIFF_MAX);
printf("SIZE_MAX = %zu\n", SIZE_MAX);
printf("SIG_ATOMIC_MIN = %+jd\n",(intmax_t)SIG_ATOMIC_MIN);
printf("SIG_ATOMIC_MAX = %+jd\n",(intmax_t)SIG_ATOMIC_MAX);
printf("WCHAR_MIN = %+jd\n",(intmax_t)WCHAR_MIN);
printf("WCHAR_MAX = %+jd\n",(intmax_t)WCHAR_MAX);
printf("WINT_MIN = %jd\n", (intmax_t)WINT_MIN);
printf("WINT_MAX = %jd\n", (intmax_t)WINT_MAX);
printf("[-%lld]-[+%lld]\n",1ll << (sizeof(int)*8 -1) ,(1ll << (sizeof(int)*8 -1)) -1 );
}
weiqifa@ubuntu:~/c/sizeof$ gcc sizeof.c && a.out
PTRDIFF_MIN = -9223372036854775808
PTRDIFF_MAX = +9223372036854775807
SIZE_MAX = 18446744073709551615
SIG_ATOMIC_MIN = -2147483648
SIG_ATOMIC_MAX = +2147483647
WCHAR_MIN = -2147483648
WCHAR_MAX = +2147483647
WINT_MIN = 0
WINT_MAX = 4294967295
[-2147483648]-[+2147483647]
weiqifa@ubuntu:~/c/sizeof$
里面提到的WCHARMIN WCHAR_MAX 关于他们的介绍如下
Numeric limits - cppreference.com
这个文件的位置在 /usr/include/limits.h
截图看看里面的内容
<img src="https://pic2.zhimg.com/v2-6207fd81c4b0f1cba66dc36700c23911_b.jpg" data-size="normal" data-rawwidth="919" data-rawheight="394" class="origin_image zh-lightbox-thumb" width="919" data-original="https://pic2.zhimg.com/v2-6207fd81c4b0f1cba66dc36700c23911_r.jpg">
图上有CHAR的大小
<img src="https://pic3.zhimg.com/v2-b4cb8b1515ddfc3ff75a981e9396a17e_b.jpg" data-size="normal" data-rawwidth="970" data-rawheight="448" class="origin_image zh-lightbox-thumb" width="970" data-original="https://pic3.zhimg.com/v2-b4cb8b1515ddfc3ff75a981e9396a17e_r.jpg">
图上有int的大小