int、short、long、long long、unsigned、unsigned short、unsigned long、unsigned long long 长度

类型标识符 32位机器所占字节长度 64位机器所占字节长度 格式化输出
int 4 4 %d
short 2 2 %d
long 4 8 %ld
long long 8 8 %lld
unsinged 4 4 %u
unsinged short 2 2 %u
unsiged long 4 8 %lu
unsigned long long 8 8 %llu

综上所述: 32位机器和64位机器比较,对于long和unsigned long类型,所占用的空间是不一样的,从代码可移植性考虑,
定义变量最好明确所需长度,选用合适的变量类型。(注:这只是常见而已,并不代表所有的32位机器和64位机器均是这样,所以选用比较明确的类型,更合理高效)

#include 
#include 

int main()
{
	int i_a = 1;
	short s_a = 2;
	long l_a = 3;
	long long ll_a = 4;
	unsigned u_a = 5;
	unsigned short us_a = 6;
	unsigned long ul_a = 7;
	unsigned long long ull_a = 8;

	printf(" i_a = %d, sizeof(i_a) = %u\n", i_a, sizeof(i_a));
	printf(" s_a = %d, sizeof(s_a) = %u\n", s_a, sizeof(s_a));
	printf(" l_a = %ld, sizeof(l_a) = %u\n", l_a, sizeof(l_a));
	printf(" ll_a = %lld, sizeof(ll_a) = %u\n", ll_a, sizeof(ll_a));
	printf(" u_a = %u, sizeof(u_a) = %u\n", u_a, sizeof(u_a));
	printf(" us_a = %u, sizeof(us_a) = %u\n", us_a, sizeof(us_a));
	printf(" ul_a = %lu, sizeof(ul_a) = %u\n", ul_a, sizeof(ul_a));
	printf(" ull_a = %llu, sizeof(ull_a) = %u\n", ull_a, sizeof(ull_a));

	return 1;
}

X86 VS测试结果:
int、short、long、long long、unsigned、unsigned short、unsigned long、unsigned long long 长度_第1张图片

你可能感兴趣的:(C)