经典笔试题:用C写一个函数测试当前机器大小端模式
生活随笔
收集整理的這篇文章主要介紹了
经典笔试题:用C写一个函数测试当前机器大小端模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
“用C語言寫一個函數測試當前機器的大小端模式”是一個經典的筆試題,如下使用兩種方式進行解答:
1. 用union來測試機器的大小端
1 #include <stdio.h> 2 3 union test 4 { 5 int a; 6 char b; 7 }; 8 9 int endian_test(void) 10 { 11 union test t1; 12 t1.a = 1; 13 return t1.b; 14 } 15 16 int main(void) 17 { 18 int i = endian_test(); 19 if(i == 1) 20 { 21 printf("is little endian.\n"); 23 } 24 else 25 { 26 printf("is big endian.\n"); 28 } 29 30 printf("i = %d.\n", i); 31 32 return 0; 33 }2. 用指針測試機器大小端
1 #include <stdio.h> 2 3 int main() 4 { 5 int a = 1; 6 char b = *((char *)&a); 7 8 return 0; 9 }注: 通信系統中,通信雙方數據傳送方式中,先發低字節的方式叫小端,先發高字節的方式叫大端。
?
轉載于:https://www.cnblogs.com/CYP01/p/6416263.html
總結
以上是生活随笔為你收集整理的经典笔试题:用C写一个函数测试当前机器大小端模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker使用阿里云docker镜像加
- 下一篇: 使用easyui框架form控件,单选按