Union and endianness
问题 typedef union status { int nri; char cit[2]; }Status; int main() { Status s; s.nri = 1; printf("%d \n",s.nri); printf("%d,%d,\n",s.cit[0],s.cit[1]); } OUTPUT: 1 0,1 I know this output on the second line is depend on the endianess of the CPU. How I can write such in a platform-independant program? Is there any way of checking the endianess of the CPU? 回答1: You can use htonl() and/or ntohl(). htonl() stands for "host to network long", while ntohl() stands for "network to host long". The "host"