判断网络字节序
- #include <stdio.h>
- typedef union{
- unsigned short value;
- unsigned char bytes[2];
- }Test;
- int main(void)
- {
- Test test_value;
- test_value.value = 0x1234;
- if(test_value.bytes[0] == 0x12 && test_value.bytes[1] == 0x34)
- printf("big ending");
- else if(test_value.bytes[0] == 0x34 && test_value.bytes[1] == 0x12)
- printf("little ending");
- else
- printf("use test_value error");
- return 0;
- }
判断操作系统32、64
判断指针
1 #include<stdio.h>
2 int main()
3 {
4 int n=sizeof(char*);
5 if(n==4)
6 printf("%d\n",n);
7 if(n==8)
8 printf("%d\n",n);
9 }
~
~
~
~

本文提供了一个简单的C程序实例,用于检测计算机系统的字节序(大端或小端)以及指针大小,以此判断操作系统的位数(32位或64位)。这些信息对于理解数据在网络传输中的表示方式及编写跨平台软件至关重要。
1049

被折叠的 条评论
为什么被折叠?



