写个程序判断系统是多少位,是大端还是小端
2017-10-29 21:20
281 查看
一句话总结:位数用指针判断,大小端用union(联合体、共用体)。
#include <stdio.h>
int main()
{
int a = 0x12345678;
printf("Os is %ld\n", sizeof(int*));
printf("a's size is %ld\n", sizeof(a));
printf("int is %ld\n", sizeof(int));
printf("short is %ld\n", sizeof(short));
union ut
{
short s;
char c[2];
};
if(2 == sizeof(short))
{
ut u;
u.s = 0x0102;
if (0x01 ==u.c[0] && 0x02 == u.c[1])
printf("big endian\n");
else
printf("little endian\n");
}
return 0;
}
zjy@ubuntu:~$ vim test.cpp
zjy@ubuntu:~$ g++ -o test test.cpp (int大小32位,超过后提示overflow)
test.cpp: In function ‘int main()’:
test.cpp:4:10: warning:
overflow in implicit constant conversion [-Woverflow]
int a = 0x1234567812345678;
^
zjy@ubuntu:~$ vim test.cpp
zjy@ubuntu:~$ g++ -o test test.cpp
zjy@ubuntu:~$ ./test
Os is 8
a's size is 4
int is 4
short is 2
little endian
zjy@ubuntu:~$
#include <stdio.h>
int main()
{
int a = 0x12345678;
printf("Os is %ld\n", sizeof(int*));
printf("a's size is %ld\n", sizeof(a));
printf("int is %ld\n", sizeof(int));
printf("short is %ld\n", sizeof(short));
union ut
{
short s;
char c[2];
};
if(2 == sizeof(short))
{
ut u;
u.s = 0x0102;
if (0x01 ==u.c[0] && 0x02 == u.c[1])
printf("big endian\n");
else
printf("little endian\n");
}
return 0;
}
zjy@ubuntu:~$ vim test.cpp
zjy@ubuntu:~$ g++ -o test test.cpp (int大小32位,超过后提示overflow)
test.cpp: In function ‘int main()’:
test.cpp:4:10: warning:
overflow in implicit constant conversion [-Woverflow]
int a = 0x1234567812345678;
^
zjy@ubuntu:~$ vim test.cpp
zjy@ubuntu:~$ g++ -o test test.cpp
zjy@ubuntu:~$ ./test
Os is 8
a's size is 4
int is 4
short is 2
little endian
zjy@ubuntu:~$
相关文章推荐
- 如何使用程序判断系统是大端还是小端?
- 写一个程序判断当前系统存储模式是大端还是小端
- 简单的C程序判断系统是大端还是小端方式
- 怎么判断32位系统下是大端还是小端
- 判断系统是大端还是小端
- 判断电脑系统中一个数是大端存储还是小端
- 写一个函数判断系统是大端还是小端
- 编写程序判断你的电脑存储方式是大端存储还是小端
- 写一个程序判断机器的的数据表示采用的是大端还是小端?
- 判断电脑是大端系统还是小端系统
- 判断系统CPU是大端还是小端 详解
- 【写一个C程序验证系统是32bit/64bit,验证系统是大端对齐还是小端对齐】
- 写个程序判断内存是大端模式还是小端模式
- 判断系统CPU是大端还是小端
- 【编程1】写一个函数判断系统是大端还是小端
- [基本] 写一个程序辨别系统是大端还是小端程序
- 一个程序判断CPU是大端还是小端
- (实训第二天)用C程序判断机器是大端模式还是小端模式,附源程序
- 用编程来判断系统是大端还是小端
- 什么是大小端模式,写个程序判断本机是大端还是小端模式